plotting columns in a table
조회 수: 33 (최근 30일)
이전 댓글 표시
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
dpb
2022년 5월 10일
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.
채택된 답변
추가 답변 (4개)
Prakash S R
2022년 5월 10일
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
Adam Jurhs
2022년 5월 11일
편집: Adam Jurhs
2022년 5월 11일
댓글 수: 3
dpb
2022년 5월 11일
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
2022년 5월 11일
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
2022년 5월 11일
댓글 수: 1
dpb
2022년 5월 11일
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
2022년 5월 11일
댓글 수: 1
Prakash S R
2022년 5월 11일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!