How to use multiple set of variables in a calculation and store the results?
5 次查看(过去 30 天)
显示 更早的评论
I have multiple sets of variables that need to be used in a calculation. I have to select each one manually by uncommenting the one I need. My code looks like this:
%% Set 1 % This is the set currently being used.
cable = 'Merlin';
cct = 5.673133;
diametro = 0.0173736;
Res_50 = 0.18875;
RMG = 0.00676656;
%% Set 2 % The rest of them are all commented
% cable = 'Osprey';
% cct = 8.286702;
% diametro = 0.0223266;
% Res_50 = 0.114543195;
% RMG = 0.00865632;
%% Set 3
% cable = 'Ortolan';
% cct = 12.10432;
% diametro = 0.0308102;
% Res_50 = 0.062834058;
% RMG = 0.01225296;
%% Set 4 and so on...
% Huge calculation happens here.
% Results from the current set are displayed.
This is obviously very inneficient. I want my program to run through each one of the sets and then store the results in an organized manner on a matrix or table, like so:
Set | Name | Important number | Some other number | etc...
1 | Merlin | 123 | 456 |
I'm sure this is a common problem but I couldn't find an adequate answer since I'm not well versed in programming lingo.
Thank you!
0 个评论
采纳的回答
Stephen23
2022-4-6
编辑:Stephen23
2022-4-6
Your current approach should be avoided: in general it is not considered good code design to store lots of data in executable code.
The best solution would be to save the data in a data file and import that. For example I moved your data to a text file (attached):
T = readtable('data.txt')
As an alternative you could store the data in any kind of iterable array (which could be a container array). For example, using a structure array make it trivial to loop over your data:
%% Set 1 % This is the set currently being used.
S(1).cable = 'Merlin';
S(1).cct = 5.673133;
S(1).diametro = 0.0173736;
S(1).Res_50 = 0.18875;
S(1).RMG = 0.00676656;
%% Set 2 % The rest of them are all commented
S(2).cable = 'Osprey';
S(2).cct = 8.286702;
S(2).diametro = 0.0223266;
S(2).Res_50 = 0.114543195;
S(2).RMG = 0.00865632;
%% Set 3
S(3).cable = 'Ortolan';
S(3).cct = 12.10432;
S(3).diametro = 0.0308102;
S(3).Res_50 = 0.062834058;
S(3).RMG = 0.01225296;
%% Set 4 and so on...
..
for k = 1:numel(S)
S(k).cable
S(k).cct
end
3 个评论
Stephen23
2022-4-7
"So may I ask how would you iterate through this table, perform an operation with each number variable (e.g., n*2) and output the results to another file?"
Either use a loop and indexing or use one of MATLAB's routines for operating on the rows/columns/groups of tables.
Save the resulting data to an array/table, then save that array/table using an appropriate function.
That is about a specific as I can answer your question lacking in any details. Basically what you asked is equivalent to "how do I use MATLAB", which if you want a complete answer covering all possible data processing (because you did not give any specific details) would require me to copy most of the MATLAB documentation into this comment.
I also have no idea what your background is or what experience you have: do you know what a loop is? Do you know how to use indexing? Are you proficient in Haskell?
These tutorials will teach you fundemental concepts that you will need know to use MATLAB: https://www.mathworks.com/help/matlab/getting-started-with-matlab.html
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!