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

4 ビュー (過去 30 日間)
Brett
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 件のコメント
Brett
Brett 2023 年 6 月 4 日
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.
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;
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeScope Variables and Generate Names についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by