How to relate data to other data from excel in MatLab?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I have some data from excel in column A (Species) that I need to related to column B (height)
Species Height
A B
Name 1 1.325
Name 2 3.574
Name 3 2.584
How do I relate the two so that Name 1 is related to 1.325, and etc? I have tried:
>> find(Height(Name1))
Unrecognized function or variable
'Name1'.
>> find(Height(2))
ans =
1
>> find(Species)
Error using find
Check for incorrect argument data type or missing
argument in call to function 'find'.
>> Species=find(Species)
Error using find
Check for incorrect argument data type or missing
argument in call to function 'find'.
>> Height(index)
Unrecognized function or variable 'index'.
Did you mean:
>> Height(rsindex)
Not enough input arguments.
>> Species=find(Species)
Error using find
Check for incorrect argument data type or missing
argument in call to function 'find'.
>> Species=Species
Species =
3×1 string array
"Name 1"
"Name 2"
"Name 3"
>> find("Name1"(Height)
find("Name1(Height))
↑
Invalid expression. When calling a function or
indexing a variable, use parentheses. Otherwise,
check for mismatched delimiters.
>> find(Height("Name1"))
Unable to use a value of type string as an index.
>> find(Height(Name1))
Unrecognized function or variable
'Name1'.
>> find(Species("Name1"))
Error using subsindex
Unable to use a value of type string as an index.
>> find(Species(Name1))
Unrecognized function or variable
'Name1'.
>> Name1=Species(Name1)
Unrecognized function or variable
'Name1'.
>> find(N1(Height)
Array indices must be positive integers or logical
values.
>> find(Height)
ans =
1
2
3
>> Height
Height =
1.325
3.574
2.584
and so on. Some of it is repetitive. I'm not sure where to go from here. Thank you.
0 commentaires
Réponses (1)
Dave B
le 14 Sep 2021
Modifié(e) : Dave B
le 14 Sep 2021
In general, it's better to think of it not as Height(Name1), but as Height(Species=="Name 1"), this extends to all sorts of datatypes, you could do Species(Height==...
If you use readtable to bring your data into MATLAB, you can take this approach:
t=readtable('trees.xlsx');
t.Height(strcmp(t.Species,'Douglas Fir'))
% easier with strings:
t=readtable('trees.xlsx','TextType','string');
t.Height(t.Species=="Black Spruce")
% Consider using categorical when it comes time to plot?
bar(categorical(t.Species),t.Height)
If your data is in two variables, it's very similar:
Height=t.Height;
Species=t.Species;
Height(Species=="White Spruce")
There are actually a few ways that you can get indexing that looks a tiny bit like what you were expecting. Here's one that works with tables:
t.Properties.RowNames=t.Species;
t.Height("Balsam Fir")
I don't think you want to go down this path, making this from a table will just be extra work, but you might have data in a one-element struct like this (spaces not allowed in this case):
s=struct('WhiteSpruce',250,'BlackSpruce',60,'DouglasFir',80,'BalsamFir',80);
s.WhiteSpruce
2 commentaires
Dave B
le 14 Sep 2021
I think you're missing a few things here:
- It looks to me like what's on the x axis is invasive and noninvasive species, but you haven't mentioned that in your data.
- When you plotted "Matrixlog" you specified that you wanted you y values to be the log of the heights, but you didn't specify x values, so MATLAB just made them 1,2,3,...
- If you want the labels on the chart, there's no need to type in all of the names, you can use text to create labels.
Here's an example with some similar data, but I'm hoping you can extrapolate for your problem:
names = ["Adam" "Bob" "Charlie" "Delilah" "Ellen" "Francine"];
sex = ["Male" "Male" "Male" "Female" "Female" "Female"];
height = [175 170 177 172 160 155];
x = double(sex=="Female");
scatter(x, log(height), 'd')
text(x(sex=="Male")-.1,log(height(sex=="Male")),names(sex=="Male"),'HorizontalAlignment','right')
text(x(sex=="Female")+.1,log(height(sex=="Female")),names(sex=="Female"),'HorizontalAlignment','left')
xlim([-.5 1.5])
xticks([0 1])
xticklabels(["Male" "Female"])
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!