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!