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

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.

 Accepted Answer

Instead of control-F try Control-H and use the Replace All button.

4 Comments

Unfortunately, that doesn't help me with my problem. I have already been using that for smaller mistakes like misnaming a variable.
That also only works for the file I have open in the editor. Even if it did work for all files in a folder, I would need it to replace everything except the index position. For example:
if array(1) >= 10
% Imagine there is something happening here
end
Let's say I am making 'array' a cell array. This is what I need my code to be:
if array{1} >= 10
% Imagine there is something happening here
end
Is there a way to search multiple files and change the "array(" + ")" to be "array{" + "}" ?
My understanding of cell arrays tells me that I cannot leave the parentheses because if I do, the class will be 'cell' when I need it to be 'double' or 'string'.
Hopefully this clarifies what I am asking and thank you for your time.
If you don't want to do it interactively, one-at-a-time, using ctrl-H to search for array(, and then replace it with array{ and then manually change the closing ) to }, then you'd have to write a program to do it. Check out readlines and strrep. It's pretty easy for one string. If you have multiple ones then you might make a GUI and let the user type in the string that needs replacing.
I think if I had better mastery of Matlab, I could make the cell array idea work, but I just can't seem to get the functionality I need. I think I will just go back to what I was doing. I did take a look at readlines and strrep, but I can't even get the cell array to work in the way I wanted to anyway.
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;
end
% All done reading all lines, so close the file.
fclose(fileID);
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.

Sign in to comment.

More Answers (0)

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!