Automatic building of an array
古いコメントを表示
Hey all,
Below is my function. I need to build the array automatically for the user input.
function suri1(Measuring_Data(:,7))
%User input
X = {1, 2,'Node1'; 3, 4, 'Node2'; 5, 6, 'Node3'}; % different for different users
n = 10;
d_T= 1 ;
t = 200;
S = load('Measuring_Data.mat');
f = 0:d_T:(t-1)*d_T;
p = cell2mat(X(:,1));
q = cell2mat(X(:,2));
disp(p);
disp(q);
T_Vorgabe = zeros(1,length(f)); % Row vector
for plotnumber = 1:n
c = find(p == plotnumber,1);
if ~isempty(c)
c = plotnumber;
for plotnumber1 = 1:n
c1 = find(q == plotnumber1,1);
if ~isempty(c1)
c1 = plotnumber1;
T_Vorgabe{c}(1,:) = S(c1,1); % May be worng syntax , All the values of the measuring data should be copied to T_Vorgabe as a row %vector or row cell array, c is the user input number to build the vector or cell array of different names
end
end
end
end
figure
hold('on')
for plotnumber2 = 1:n
c2 = find(p == plotnumber,1)
if ~isempty(c2)
c2 = plotnumber;
plot(f(1:15:end)/200, T_Vorgabe{c2}); % Slover should plot all the values of T_Vorgabe(row vector or row cell array.
end
end
end
Here T_Vorgabe{c2} will be cell array. Now while plotting the T_Vorgabe solver should get the all the values stored from the respective cell arrayT_Vorgabe{c2}.
My questions are,
How to make T_Vorgabe as a row vector, or row cell array of different names according to user input?
Like below
T_Vorgabe{1}=[1,2,3,...,2920]
T_Vorgabe{2}=[2921,2922,2923,...,5840] etc
Any suggestions and answers are most welcomed.
Thanks in advance
6 件のコメント
Stephen23
2019 年 6 月 28 日
"Any suggestions... are most welcomed."
Do NOT do this!
Do NOT mix meta-data into code: meta-data is data, so it should be stored in a variable, not in the variable's name. By forcing meta-data into variable names you will force yourself into writing slow, complex, buggy, obfuscated code which is hard to debug. Read this to know why:
Note that you should load the file data into an output variable (which is a scalar structure):
S = load(...)
and then access its fieldnames:
surendra kumar Aralapura mariyappa
2019 年 6 月 28 日
Bob Thompson
2019 年 6 月 28 日
Your for loop seems to be set up correctly to index the information you want into different cells, so I don't see a major problem with the concept you are employing. Can you be more specific about what is wrong with the output you're getting.
Are you getting any errors? If so, please copy the entire error message for us.
If you are not getting any error messages, then how do you know the output is incorrect? What are you receiving, and how does it differ from what you expect?
"I changed the code according to your suggestion."
Not really, because your edited question shows that you ignored the parts of my comment where I wrote "...which is a scalar structure... then access its fieldnames..." Why are you using indexing into a scalar structure? Why are you ignoring the structure's fieldnames?
"But I didn't get the answer to my real question. "
The only question you asked was "How to make T_Vorgabe as a row vector, or row cell array of different names according to user input." I did not answer that question (these are comments, not answers (see below for answers)), but instead I wrote a comment explaining why this approach is not recommended. I also showed you how it can be avoided. My aim is to help you write better code, by avoiding the inefficient approach that you are attempting.
surendra kumar Aralapura mariyappa
2019 年 6 月 28 日
編集済み: surendra kumar Aralapura mariyappa
2019 年 6 月 28 日
surendra kumar Aralapura mariyappa
2019 年 7 月 1 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!