How to relate data to other data from excel in MatLab?

2 visualizaciones (últimos 30 días)
Kristin Aldridge
Kristin Aldridge el 13 de Sept. de 2021
Comentada: Dave B el 14 de Sept. de 2021
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.

Respuestas (1)

Dave B
Dave B el 14 de Sept. de 2021
Editada: Dave B el 14 de Sept. de 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'))
ans = 80
% easier with strings:
t=readtable('trees.xlsx','TextType','string');
t.Height(t.Species=="Black Spruce")
ans = 60
% 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")
ans = 250
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")
ans = 80
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
ans = 250
  2 comentarios
Kristin Aldridge
Kristin Aldridge el 14 de Sept. de 2021
Thank you. I was trying to also avoid having to type in every species name, as they are lengthy and in Latin, which is why I didn't choose the Species1=="name1" route.
I did go on to try:
>>Matrix=[Name,Height]
"Name 1" "Height 1"
"Name 2" "Height 2"
"Name 3" "Height 3"
>>Matrixlog=log(Height)
Matrixlog =
2.70
4.67
3.54
>>plot(Matrixlog)
I'm trying to get this graph instead. I have the eigenvalues (found from a different homework set where I did use ==) which need to be plotted on the x axis, and the natural log of heights on the y axis.
Dave B
Dave B el 14 de Sept. de 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"])

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by