Convert a cell in a field from string to number

3 views (last 30 days)
Hi everyone. I'm having an issue as I'd like to graph some data but can't becuase it is in a string and dont know how to convert it to a number. I load the points from a tsv file with the following
%% Load xypts
points.samp{1}=tdfread('samp1xypts.tsv',';'); %load data points from particle tracking
points.samp{2}=tdfread('samp2xypts.tsv',';'); %load data points from particle tracking
points.samp{3}=tdfread('samp3xypts.tsv',';'); %load data points from particle tracking
points.samp{4}=tdfread('samp4xypts.tsv',';'); %load data points from particle tracking
points.samp{5}=tdfread('samp5xypts.tsv',';'); %load data points from particle tracking
I then put the data into cell arrays with the following
for i=1:1:length(frames)
ind=find(points.samp{n}.Sparse_format_camera_coordinates_file(:,1)==frames(i));
points.sorted{n}(i+1,1) = frames(i);
points.sorted{n}(i+1,2) = points.samp{n}.Sparse_format_camera_coordinates_file(ind(1),3);
points.sorted{n}(i+1,3) = points.samp{n}.Sparse_format_camera_coordinates_file(ind(2),3);
points.sorted{n}(i+1,4) = points.samp{n}.Sparse_format_camera_coordinates_file(ind(3),3);
points.sorted{n}(i+1,5) = points.samp{n}.Sparse_format_camera_coordinates_file(ind(4),3);
end
Which looks like this
I would like to graph three collums of this data using a 3D plot. Say for example I chose the first cell array I get the following error message
y = points.sorted{1}(:,2);
x = points.sorted{1}(:,3);
z = points.sorted{1}(:,5);
p = plot3(x,y,z,'-g');
Error using plot3
Not enough input arguments.
Error in sortPoints (line 33)
p = plot3(x,y,z,'-g');
I suspect it is because the data is currently in string format as it works if I copy that data table into excel.... and then back into matlab again as a number and manually set up the vectors. Any help would be greatly appreciated. Thanks :)

Answers (1)

Walter Roberson
Walter Roberson on 21 Sep 2022
I suspect it is because the data is currently in string format
x y z all need to be numeric for plot3() to work.
However, tdfread() should notice that all entries in the file are numeric other than the first row, and should create numeric output -- unless, that is, you have additional numeric rows that we cannot see from the diagram?
I recommend that you switch to using readmatrix() or readtable()
  2 Comments
Jack
Jack on 21 Sep 2022
Yes when I initially set up the cell I add headings so I know which is which.
for n = 1:5
points.sorted{n}(1,1) = "Frame";
points.sorted{n}(1,2) = "Vertical disp";
points.sorted{n}(1,3) = "Horizontal disp";
points.sorted{n}(1,4) = "Vertical disp mirror";
points.sorted{n}(1,5) = "Horizontal disp mirror";
for i=1:1:length(frames)
ind=find(points.samp{n}.Sparse_format_camera_coordinates_file(:,1)==frames(i));
points.sorted{n}(i+1,1) = frames(i);
points.sorted{n}(i+1,2) = points.samp{n}.Sparse_format_camera_coordinates_file(ind(1),3);
points.sorted{n}(i+1,3) = points.samp{n}.Sparse_format_camera_coordinates_file(ind(2),3);
points.sorted{n}(i+1,4) = points.samp{n}.Sparse_format_camera_coordinates_file(ind(3),3);
points.sorted{n}(i+1,5) = points.samp{n}.Sparse_format_camera_coordinates_file(ind(4),3);
end
end
It now works when I comment out the headings. Is there a way that I can plot all the points except the top row so I can still have the headings?? Thanks for your help :)
Walter Roberson
Walter Roberson on 21 Sep 2022
I would suggest that you convert to using table() objects.

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!