shirin
shirin

Reputation: 3

how to plot cell array with (31*1) dimensions in matlab?

I have a cell array with one column and thirty one rows, and I'm going to plot the array so that the horizontal axis changes from one to thirty one, and the vertical axis corresponds to values like peer to inside cell. my cell array :

data2 =

31×1 cell array

'2.4392E-09' '2.6506E-09' '3.0690E-09' '4.0424E-09' '7.1719E-09' '1.8084E-08' '6.0006E-08' '2.1621E-07' '7.7861E-07' '2.6695E-06' '8.4323E-06' '2.3340E-05' '5.1783E-05' '1.1155E-04' '2.6871E-04' '3.4549E-04' '2.6871E-04' '1.1155E-04' '5.1783E-05' '2.3340E-05' '8.4323E-06' '2.6695E-06' '7.7861E-07' '2.1621E-07' '6.0006E-08' '1.8084E-08' '7.1719E-09' '4.0424E-09' '3.0690E-09' '2.6506E-09' '2.4392E-09'

and

i2 =

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

and part of my code for plot is:

i=1:1:31
data2=data(:,1)
i2=transpose(i);
i2=i2(:,1)
plot(i2,data2)

Upvotes: 0

Views: 178

Answers (2)

Max
Max

Reputation: 4045

This question aims at the very basics of MATLAB. You have strings in a cell array. Access the content of cells with {} and convert it with str2doubleto numbers.

Further, keep the code clean and readable (data, data2 and i,i2) are not good variable names in no language... You don't need to transpose the vector but if you do, you can use the shortcut .'. Note that the . indicates that it is not a complex transpose

idx = 1:size(data,1)
cstr = data(:,1); % call the content of cells with {} / call a cell element with ()
num = str2double(cstr); % convert string to doubles/numbers
plot(idx.',num) % .' transposes an array/vector but in fact, you don't need it here

Upvotes: 0

Sardar Usama
Sardar Usama

Reputation: 19689

str2double converts numbers, stored as characters in cells of data2, to numeric (double) type. It is directly applicable on cell-arrays. If the required x-axis is the same as 1:numel(data2) then specifying it is not needed. So,

plot(str2double(data2));

Upvotes: 2

Related Questions