Accessing the index of a matrix element inside the cell array

14 views (last 30 days)
Hey all,
I have a small question in regarding the access of index of a matrix element inside the cell array.
A = {[1 2 3 4] [ 'Node1' 'Node2' 'Node3']};
Above is my cell array. Now I need to get the index of an element of matrix inside the cell array.
For an example if c = 3, just an example, c may be any number.
then I need it's index like A{1,1}(1,3)
Can anyone help me in this?
Thanks in advance
  1 Comment
Stephen23
Stephen23 on 1 Jul 2019
Note that square brackets are a concatenation operator, so your example
[ 'Node1' 'Node2' 'Node3']
is equivalent to:
'Node1Node2Node3'

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 1 Jul 2019
First, a few recommendations.
  • If you're using vectors use linear indexing instead of 2D indexing. I.e. X{1} instead of X{1, 1}.
  • if n<=3 ... elseif n>3 ... end, if the if is false you're guaranteed that the elseif is true. Therefore the elseif is not needed, you can simply write if n<=3 ... else ... end. In general, you should always finish by an else, not an elseif.
  • don't use the same variable name, particularly one as meaningless as c for storing to different things. In your code, c is used to store the index where plotnumber is found in the cell array, then to store plotnumber.
  • Whenever you end writing the same lines of code with only small variations between them it's a clear indication that there must be a better way, such as indexing.
In particular, with that last point, we can rewrite your code as:
[f,T1, X] = Samplemodel_Instat('Measuring_Data.mat');
% X will be like this X = {[ 2 4] [ 'Oil' 'Air']};
%Now defines the constants that will be used for plots, as 5xN matrices (as there are 5 different possible plots)
Colours = [0.831372559070587 0.815686285495758 0.7843137383461;
0.25 0.25 0.25;
0.952941179275513 0.87058824300766 0.733333349227905;
0.31 0.31 0.31;
0.831372559070587 0.815686285495758 0.7843137383461];
LineWidths = [0.5; 4; 2; 2; 0.5]; %0.5 is default for plot
LineStyles = {'-'; '-.'; '-'; '-'; '-'}; %'-' is default for plot
Markers = {'none'; 'none'; 's'; 'none'; 'none'}; %'none' is default for plit
for plotnumber = 1:n
whichmodel = find(X{1} == plotnumber,1);
if ~isempty(whichmodel)
if n <= 3
plotstyle = 1; %which row of Colours, LineWidths, etc.. to use
else
plotstyle = discretize(plotnumber, ceil((0:4)*n/4), 'IncludeEdge', 'right') + 1;
end
if ismember(plotstyle, [1 3])
plotindices = (1:150:numel(f)) / 3600;
else
plotindices = (1:200:numel(f)) / 3600;
end
plot(f(indices), T1(plotnumber, indices), ...
'Color', Colours(plotstyle, :), ...
'LineWidth', LineWidths(plotstyle), ...
'LineStyle', LineStyle{plotstyle}, ...
'Marker', Markers{plotstule}, ...
'MarkerFaceColor', [1 1 1]);
legend(X{2}(whichmodel)); %index 2nd element of cell array
end
end
  3 Comments
surendra kumar Aralapura mariyappa
Edited: Guillaume on 1 Jul 2019
I used your code, but it is showing below error.
Undefined function 'discretize' for input arguments of type 'double'.
Error in suri3 (line 20)
plotstyle = discretize(plotnumber, ceil((0:4)*n/4), 'IncludeEdge', 'right') + 1;
I searched using which functionname,
It exist in the right dictionary.
C:\toolbox\matlab\datatypes\@categorical\discretize.m % categorical method
I am not surew what is going wrong!
Can you help me in this?
I am using both R2014a and R2017b both versions, In both versions it is showing this error. May I know when this function discretize was introduced?
I am using student version. is the above function available in the student version or not?
And also on community I read to function should be exactly in the working dictionary? what does it mean?
can you tell me, is there any way to fix it?
Thanks in advance
Guillaume
Guillaume on 1 Jul 2019
discretize requires R2015a or later. You shouldn't get an error in R2017b, but you'll definitively get one on R2014a.
On R2014a, replace that line by:
[~, plotstyle] = histc(plotnumber, ceil((0:4)*n/4) + eps(n));
plotstyle = plotstyle + 1;

Sign in to comment.

More Answers (1)

Soumya Sinha
Soumya Sinha on 1 Jul 2019
Edited: Soumya Sinha on 1 Jul 2019
Hello Surendra,
Here in this case you have created a 1*2 Cell array in which first element is an array of numbers and the second one is the array of strings.
You can access the number 3 by writing the code
A{1,1}(3)
  3 Comments
Guillaume
Guillaume on 1 Jul 2019
@surendra,
Your code is working fine, so it's really not clear what problem you're having. It's not clear what you mean by the index of c (which is scalar).
surendra kumar Aralapura mariyappa
@ Guillaume,
' It's not clear what you mean by the index of c (which is scalar)'
Okay, I am going to put my code here. I already asked for the answer in the community, waiting for the answer yet. Again I am going to ask here also.
You can see in the below code. Accoding to the Values in the Cell array X{1,1} plot will be plotted. But I need to give legend to each plot everytime. So Here is the user input cell array
[f,T1, X] = Samplemodel_Instat('Measuring_Data.mat');
% X will be like this X = {[ 2 4] [ 'Oil' 'Air']};
for plotnumber = 1:n
c = find(X{1,1} == plotnumber,1);
if ~isempty(c)
c = plotnumber;
if n <= 3
plot(f(1:150:end)/3600,T1(c,1:150:end),'Color',[0.831372559070587 0.815686285495758 0.7843137383461]);
elseif n > 3
if c >= 1 && c <= ceil(n./4)
plot(f(1:200:end)/3600,T1(c,1:200:end),'Color',[0.25 0.25 0.25],'LineWidth',4,'LineStyle','-.','MarkerFaceColor',[1 1 1],'Marker','none');
%legend()
elseif c > (ceil(n./4)) && c <= ceil(n./2)
plot(f(1:200:end)/3600,T1(c,1:200:end),'Color',[0.952941179275513 0.87058824300766 0.733333349227905],'LineWidth',2,'MarkerFaceColor',[1 1 1],'Marker','s');
%legend()
elseif c > (ceil(n./2)) && c <= ceil(3*n./4)
plot(f(1:200:end)/3600,T1(c,1:200:end),'Color',[0.31 0.31 0.31],'LineWidth',2,'LineStyle','-','Marker','d','MarkerFaceColor',[1 1 1]);
%legend()
elseif c > (ceil(3*n./4)) && c <= n
plot(f(1:150:end)/3600,T1(c,1:150:end),'Color',[0.831372559070587 0.815686285495758 0.7843137383461]);
%legend()
end
end
end
end
So here According to the cell array, plot will be plotted, but I need to give the name to each plot using legend. For an example if c = 2, legend will be oil. like this I needed.
I thought of doing it in another way. But I got to know it is not possible.
The question is very simple, how to get the legend according to c. so here c is X{1,1} and legend is X{1,2}.
I hope that now at least it is understandable.
Thanks in advance

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2014a

Community Treasure Hunt

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

Start Hunting!