Having trouble finding a row in a table

%I'm trying to load this table and then search for a number within the first column, then load that entire row, I can get it if I use this example: table(5,[1:5]) manually, but cannot get my input to be found and then given to me. I get this error "Operator '==' is not supported for operands of type 'table'." and " Error in e=find(a(:,1)==X);"
load('table.mat');
X=input('What would you like to know?: ');
row=find(table(:,1)==X);
disp('The properties at this temperature are:');
table(row,[1:5])

2 commentaires

You show the error as
Error in e=find(a(:,1)==X);
but use a different variable name here
row=find(table(:,1)==X);
Any reason why you have different variable names a and table in different places of code? Please show relevant code and /or data
Image Analyst
Image Analyst le 6 Avr 2023
Three people (below) have tried to help you but it seems like we're spinning out wheel because you keep forgetting to attach your table. If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 6 Avr 2023

1 vote

We do not have ‘a.mat’ to work with.
That aside, it may contain a struct (structure array), and if so, the struct2table function could be a useful initial approach.
You could then use table indexing into the table that function creates.

4 commentaires

Edgar
Edgar le 6 Avr 2023
Here is the flie, sorry for not thinking of uploading it before.
No worries!
Try something like this —
LD = load(websave('a','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1347779/a.mat'))
LD = struct with fields:
a: [76×5 table]
a = LD.a
a = 76×5 table
Temperature (C) Pressure(kPa) vf (m^3/kg) vfg (m^3/kg) vg (m^3/kg) _______________ _____________ ___________ ____________ ___________ 0.01 0.6117 0.001 206 206 5 0.8725 0.001 147.03 147.03 10 1.2281 0.001 106.32 106.32 15 1.7057 0.001001 77.884 77.885 20 2.3392 0.001002 57.761 57.762 25 3.1698 0.001003 43.339 43.34 30 4.2469 0.001004 32.878 32.879 35 5.6291 0.001006 25.204 25.205 40 7.3851 0.001008 16.514 16.515 45 9.5953 0.00101 15.25 15.251 50 12.352 0.001012 12.025 12.026 55 15.763 0.001015 9.5629 9.5639 60 19.947 0.001017 7.666 7.667 65 25.043 0.00102 6.1925 6.1935 70 31.202 0.001023 5.0386 5.0396 75 38.597 0.001026 4.1281 4.1291
VN = a.Properties.VariableNames;
X = 35;
row=find(a.('Temperature (C)')==X)
row = 8
rowinterp = a{row,:}; % Specific Matching Row
fprintf('The properties at this temperature are: \n%s\t%s\t%s\t%s\t%s\n\t%3d\t%.3f\t\t%.6f\t%.4f\t\t%.4f',VN{:},rowinterp)
The properties at this temperature are: Temperature (C) Pressure(kPa) vf (m^3/kg) vfg (m^3/kg) vg (m^3/kg) 35 5.629 0.001006 25.2040 25.2050
T = 42; % Interpolated Values
rowinterp = [T interp1(a{:,1},a{:,2:5},T)];
fprintf('The properties at this temperature are: \n%s\t%s\t%s\t%s\t%s\n\t%3d\t%.3f\t\t%.6f\t%.4f\t\t%.4f',VN{:},rowinterp)
The properties at this temperature are: Temperature (C) Pressure(kPa) vf (m^3/kg) vfg (m^3/kg) vg (m^3/kg) 42 8.269 0.001009 16.0084 16.0094
Experiment to get different results.
.
Edgar
Edgar le 6 Avr 2023
Thank you so much! I've been struggling for days !
Star Strider
Star Strider le 6 Avr 2023
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Connectez-vous pour commenter.

Plus de réponses (1)

You need to extract the contents of the table variable if you want to use == on it in the release you're using.
load patients
T = table(LastName, Height, Weight);
firstTenRows = T(1:10, :)
firstTenRows = 10×3 table
LastName Height Weight ____________ ______ ______ {'Smith' } 71 176 {'Johnson' } 69 163 {'Williams'} 64 131 {'Jones' } 67 133 {'Brown' } 64 119 {'Davis' } 68 142 {'Miller' } 64 142 {'Wilson' } 68 180 {'Moore' } 68 183 {'Taylor' } 66 132
Let's find the patients that are 68 inches tall.
patients68 = firstTenRows.Height == 68; % or
patients68 = firstTenRows{:, 'Height'} == 68;
selectedPatients = firstTenRows(patients68, :)
selectedPatients = 3×3 table
LastName Height Weight __________ ______ ______ {'Davis' } 68 142 {'Wilson'} 68 180 {'Moore' } 68 183
If you were using release R2023a or later and all the data in your table supported the == operator you could directly perform certain math operations on the table.
justHeightAndWeight = firstTenRows(:, ["Height", "Weight"])
justHeightAndWeight = 10×2 table
Height Weight ______ ______ 71 176 69 163 64 131 67 133 64 119 68 142 64 142 68 180 68 183 66 132
justHeightAndWeight(:, "Height") == 68
ans = 10×1 table
Height ______ false false false false false true false true true false

Catégories

Produits

Version

R2022b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by