MATLAB Answers

Automatic building of an array

10 views (last 30 days)
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 Comments

Show 3 older comments
Stephen Cobeldick
Stephen Cobeldick on 28 Jun 2019
"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
@Nbob ''Can you be more specific ''
T_Vorgabe{c}(1,:) = S(c1,1);
I just need to know whether it is correct or not!. Why because, currently I don't have measuring data with me. So I need to build it for the future use.
Let's be more specific.
I have a matrix S contains measuring data. So I need to copy the specific row of S into T_Vorgabe according to user input. For an example, If the user want to copy row 2 5 9 to T_Vorgabe, T_Vorgabe should be a row vector or row cell array of different names. Like below
T_Vorgabe{2}(1,:)
T_Vorgabe{5}(1,:)
T_Vorgabe{9}(1,:)
Here the problem is, Whether the code is correct or not! Both T_Vorgabe{c}(1,:) = S(c1,1); and T_Vorgabe{2}(1,:) .
Second question is How to access the indicies of element of cell array?
For an example.
X = {1, 2,'Node1'; 3, 4, 'Node2'; 5, 6, 'Node3'};
p = cell2mat(X(:,1));
Now I need to access the indicies of cell element of first column.
I hope my explanation is okay now to get the answer.
Thanks in advance.
surendra kumar Aralapura mariyappa
Now I realized the importance of your suggestion after I ran my program. It is taking too much time to do compilation. It is the time for me to make my code according to your suggestion.
Anyways thank you.

Sign in to comment.

Accepted Answer

Bob Nbob
Bob Nbob on 28 Jun 2019
'T_Vorgabe{c}(1,:) = S(c1,1);
I just need to know whether it is correct or not!. '
As I mentioned before, the concept is sound, and it should produce something like what you are looking for. I cannot tell you if it is 'correct' because minor adjustments may be needed to personalize the code to your specific dataset. I suspect this line will not actually have to be changed, but perhaps your definition of c and c1 will need to be changed. That is something you will have to tackle though when you have an actual sample of data to work with. That's just how debugging works.
'Second question is How to access the indicies of element of cell array?'
For information on how to work with cell indices I recommend this link. If you still need additional help, please be more specific.

  1 Comment

surendra kumar Aralapura mariyappa
@ Nbob, ' please be more specific.'
I think at least I will get the solution to my problem now.
%T1(:,1) = T1(:,1)+ 20; % Adding the ambient temperature
%f = 0:d_T:(t-1)*d_T; %d_T is step time
% Here d_T = 1 and t = 200 iterations. %T1 is a temperature vector for 200 iterations
load('Measuring_Data.mat');
[f,T] = Samplemodel_Instat('Measuring_Data.mat');
[Lambda, A, B, D,V,H, Rho,cP,P, M, A1,O, X, M_1] = Matrix_Samplemodel;
% X = { 3 6 'Node1': 4 9 'Node2'}; Like this
p = cell2mat(X(:,1));
q = cell2mat(X(:,2));
disp(p);
disp(q);
n = size(A,1);
T_Vorgabe = zeros(1,length(f)); % I am not sure whether the preallocation is correct or not!.
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,:) = Measuring_Data(c1, 1); % Here Measuring_Data should be copied to %T_Vorgabe each time with new variable name according to the user input.
end
end
end
end
%then all the T_Vorgabe{c} should be plotted.
plot(f(1:200:end)/3600,T_Vorgabe{c}(1:200:end),'Color','k','LineWidth',2,'LineStyle','--');
end
I hope now it is understandable.
this is the error I am getting:
"Cell contents assignment to a non-cell array object.
Error in suri1 (line 22)
T_Vorgabe{c}(1,:) = Measuring_Data(c1, 1);"
I just need to copy the measuring values to new T_Vorgabe variable like T_Vorgabe.1, T_Vorgabe.2 each time and plot it.
Any suggestions and answered are most welcomed
Thanks in advance.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!