plotting columns in a table

28 visualizzazioni (ultimi 30 giorni)
Adam Jurhs
Adam Jurhs il 10 Mag 2022
Risposto: Adam Jurhs il 12 Mag 2022
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 Commento
dpb
dpb il 10 Mag 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.

Accedi per commentare.

Risposta accettata

Adam Jurhs
Adam Jurhs il 12 Mag 2022
yep Prakash, getting the syntax right was the problem, but with your help i did figure it out.
thanks!
Todd

Più risposte (4)

Prakash S R
Prakash S R il 10 Mag 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

Adam Jurhs
Adam Jurhs il 11 Mag 2022
Modificato: Adam Jurhs il 11 Mag 2022
Prakash, your way of importing the csv file worked, thnaks! I had been using the Import Data wideget in 2021b and it made a mess of things when it imported the csv (eventually I did figure out how to make the wideget import the file as both doubles and cell arrays.
  3 Commenti
dpb
dpb il 11 Mag 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
Prakash S R il 11 Mag 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)

Accedi per commentare.


Adam Jurhs
Adam Jurhs il 11 Mag 2022
dpb - i would give you an example file if i could, but the data resides on a system that is not connected to the internet, so i have to fumble along like this, sorry! since you asked what i'm after, my end goal will be an error bar plot, but i'm taking one step at a time...
Prakash - i have made sure that the XTick of the doubles and the XTick of the Strings are the same, in this example 3. what i dont know how to do is pass the 2nd column strings into the XTickLabel without having to type them in by hand (with 20 rows this would be impracticle), i'm thinking something like this
set(gca, 'xticklabels',{string(:,2)})
but of course that doesent work, thanks for all your help, i really do appreciate it!!!
  1 Commento
dpb
dpb il 11 Mag 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.

Accedi per commentare.


Adam Jurhs
Adam Jurhs il 11 Mag 2022
dpb, i wish i could post the data, but i don't have a way to, i can tell that's frustrating to you, i'm sorry!
the data file looks exactly as Prakash outlined above 3 strings and 3 numerics, and i have imported the file using the Import Data widget getting a 13:3 Numeric Matrix of doubles and a 13:3 Cell Array of strings (other files will give me more than 13 rows).
i've gotten pretty far along in my goal of making an error plot, and i can do it if i type (by hand) all 13 strings using XTickLabels. What i need to do now is learn how to pass str2 into XTickLabels. To automate this i've tried set(gca, 'xticklabels',{str2(:,2)}) but that of course that doesn't work.
I appreciate the help you've given!
  1 Commento
Prakash S R
Prakash S R il 11 Mag 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

Accedi per commentare.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by