Turning a double array into a cell array in a big program

1 回表示 (過去 30 日間)
Brett 2023 年 6 月 1 日
コメント済み: Image Analyst 2023 年 6 月 4 日
Hello. I am a fledgling programmer and I have been writing a big program (for me) for a few weeks now and I have realized that one of my main input arguments within my functions needs to be changed into a cell array. I decided this was probably the right decision because I want to reduce my input arguments to just a few variables instead of several. The problem here is that I have a double array and strings that need stored, so I thought a cell array would be the best option (I also thought about a structure but that would make my issue a lot worse).
This would be no problem, but I have already written 60 or 70 functions that all use this input argument as a double array. When I have had issues similar to this in the past, I would usually just CTRL+F and replace all. The issue here is that it would not be very fun to go through and change 250+ calls for a variable. As far as I know, I would have to be really tedious with CTRL+F because my understanding of indexing cell arrays uses {#} instead of (#) like a normal array. I don't know of a way to extend my CTRL+F into more than just the file I have open, and I don't know how to 'replace all' but leave the matrix indeces.
I don't have a lot of Matlab experience, but so far I have been able to find all the answers to my questions in the Matlab documentation and here in the answers section. I am writing this program with the purpose of learning more about Matlab and programming in general, so I don't fully understand the terminology yet. My only real experience with Matlab in an academic setting is Differential Equations and introductory zyBooks Matlab course.
I would be really grateful for any advice on mass-replacing and/or any tips on cell arrays vs. double/string arrays.


Image Analyst
Image Analyst 2023 年 6 月 1 日
Instead of control-F try Control-H and use the Replace All button.
  4 件のコメント
Image Analyst
Image Analyst 2023 年 6 月 4 日
Cell arrays can use both (), which refers to the cell, or {} which refers to the CONTENTS of the cell. See the FAQ:
You might think to try this
fullFileName = 'test.m';
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('Old Line: %s\n', textLine);
% Change ( to {
textLine = strrep(textLine, '(', '{');
% Change ) to }
textLine2 = strrep(textLine, ')', '}');
fprintf('New Line: %s\n', textLine2);
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
% All done reading all lines, so close the file.
but the problem is it will change parentheses into braces for functions also, which you definitely do not want to do. You want to do it just for variables, so you'd have to input a list of variable names and then use contains to see if the line contains that variable. But it can get more complicated that that. For example if a line has a function where you pass in that variable.
Anyway, please read the FAQ.


その他の回答 (0 件)


Help Center および File ExchangeGet Started with MATLAB についてさらに検索




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by