plotting columns in a table
33 views (last 30 days)
Show older comments
I have a csv file that when i import it (using the import tool) it imports it as a table. Columns 1-5 are strings, but columns 6, 7, and 8 are doubles. I need to plot columns 6, 7, and 8 without setting the table as a double. If I set it as a double it messes up all the columns and of course looses the strings in columns 1-5, and I will need those columns for future operations.
1 Comment
dpb
on 10 May 2022
This makes no sense -- what would columns be but doubles?
I've never even looked at the import tool, but if you have mixed data a table is the obvious data type to use but we're unable to see your terminal from here so we have no idea what it actually did...nor what you expected/want, precisely.
For starters, what does
head(WhateverVariableIsYourTable)
return? That at least shows us something from which to see what you have and where there might be a problem.
If you mean that the import tool brings in everything including the numeric data as character/string data, then probably there are row headers or somesuch that the automagic file scan didn't recognize them as numeric. You can convert any given column to whatever type need independent of the others; that's why tables are good for heterogenous data. Of course, the content will have to be something that str2double can convert or you'll just get NaN.
Accepted Answer
More Answers (4)
Prakash S R
on 10 May 2022
How are you "importing" it? If you use importdata(), the string columns and the double columns go into separate arrays, as do the column headers, if any.
So if your csv file (say foo.csv) looks like this:
str1, str2, str32, 3, 1, 2.2
str1, str2, str31, 3.1, 4.2, 5
str1, str2, str3, 3, 4, 5
str1, str2, str3, 3, 4, 5
str1, str2, str3, 3, 4, 5
Then
C = importdata('foo.csv', ',');
will give you a struct with .data being a 5x3 numerical array of doubles and .textdata containing the 5x3 cell array of strings
0 Comments
Adam Jurhs
on 11 May 2022
Edited: Adam Jurhs
on 11 May 2022
3 Comments
dpb
on 11 May 2022
This would be SO much easier if you would attach the data file -- and use readtable instead of the clumsy importdata
Everything would have its own name as variables and turning the cellstr data into categorical would in all likelihood give you the desired labeling in the plots for free.
You've still not illustrated just what you're trying to plot against what so we're left guessing...
"Help us help you..." -- give us the data and tell us EXPLICITLY what you're after.
Prakash S R
on 11 May 2022
I hope I understand correctly:
After importing the .csv with N rows and 6 columns, the string columns were correctly read into a Nx3 cell array S, and the doubles into a Nx3 matrix D.
You plotted the columns of D, and now you want to use the N strings in S{:,2} as the xticklabels
Your first task is to make sure that the number of xTicks is in fact N. (since the xTicks are assigned by the plotting function, and depends on the size of the figure etc., there is no reason why it should be the case).
You can force this by setting XTick: set(gca, 'XTick', D(:,2), 'XTickLabel', S{:,2})
Then you'll have N labels. (Which, if N is large enough, may give you a horrible looking figure, but that is a problem you can solve by showing every M'th tick or something like that)
Adam Jurhs
on 11 May 2022
1 Comment
dpb
on 11 May 2022
What happens if you do
tData=readtable('foo.csv');
whos tData
head(tData)
Show/tell us the result of the above -- it should be a MUCH cleanear approach.
That will give us a much clearer picture of the file content...
Again, what is(are) the specific x- and y- variable(s) intended to plot? If, indeed it is plotting against the variables of the strings, turning them into categorical and using those directly will bring along the labels for free.
But, we still are pretty-much in the dark as to what we have to work with and what is the objective.
You could type in a few lines directly at the command line that would mimic the data content it would seem; head will throw out the first eight lines; that would be a good start -- wouldn't even have to be that many lines, probably.
Adam Jurhs
on 11 May 2022
1 Comment
Prakash S R
on 11 May 2022
What do you mean by "that doesn't work"? What error are you getting?
In your example above, why did you do {} around str2(:,2)? That will give you a 1x1 cell array containing a 13x1 cell array. str2(:,2) already is the array you need
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!