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!

采纳的回答

Stephen23
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')
T = 3×5 table
cable cct diametro Res_50 RMG ___________ ______ ________ ________ _________ {'Merlin' } 5.6731 0.017374 0.18875 0.0067666 {'Osprey' } 8.2867 0.022327 0.11454 0.0086563 {'Ortolan'} 12.104 0.03081 0.062834 0.012253
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
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
LoroEE
LoroEE 2022-4-7
You are right, I'll accept your answer and think about a more specific question.
I'm an engineering student, my programming knowledge is all over the place and I still consider myself a begginer. I do know what a loop and indexing is. I also know what needs to be done, the problem is telling Matlab to do it, particularly the interaction between the loops and indexing and file output.
I could probably figure out a very idiotic way of doing this, but it will take days and I rather just ask. Documentation is really obtuse and I feel the beginner examples don't really cut it anymore.
Anyways, just getting this out there. I'm still in the process of figuring out how to ask (and find) good questions.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by