0

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)
5
  • 1
    it appears that the numbers in your cell are character/strings. This is not a minimal working example (post code not the ouput on the command line window) Commented Mar 7, 2020 at 17:50
  • my code is:i=1:1:31 data2=data(:,1) i2=transpose(i); i2=i2(:,1) plot(i2,data2) Commented Mar 7, 2020 at 17:56
  • and how is data created? What data type is in there? Commented Mar 7, 2020 at 17:57
  • These data are created by calling a text file with a command textscan:data = textscan(fid, '%s', 'Delimiter', '\n'); Commented Mar 7, 2020 at 18:00
  • 1
    Why not read with %f instead of %s? That way you should get a normal numeric array that you can directly plot. Commented Mar 7, 2020 at 18:43

2 Answers 2

2

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));
Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

thank you .your code is very good ..but num = str2double(str);Just returns the last row of the cell presentation and I need to have all the rows to plot
@shirin sorry, you can apply str2double directly on a cell. I corrected the code. It should work now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.