Access data from table and assign to variables

Hi, I am making a program with excel file already converted to table and now I need to code such that when a user gives an input a certain coloumn is taken and then assign certain variables to some particular blocks in that row.
example:
Vehicle speed fuel
Car 50 2
Bike 20 1
Cycle 5 0
Like user gives input as Bike. then I need to assign variables created like speed and fuel to use in further equations.

4 件のコメント

Image Analyst
Image Analyst 2021 年 8 月 27 日
What type of variable do you want to create? A new table? Add a row to an existing table? A new instance of a class variable? A new structure, perhaps added onto an existing structure array?
Dhananjay Singh
Dhananjay Singh 2021 年 8 月 27 日
No changes to the table . I want to extract other values like speed and fuel based on the input given by user(eg Bike). and use them for further calculations.
Stephen23
Stephen23 2021 年 8 月 27 日
"...then I need to assign variables created like speed and fuel to use in further equations."
Why not just access the data directly from the table?
Dhananjay Singh
Dhananjay Singh 2021 年 8 月 27 日
thank you for answering its clear now

サインインしてコメントする。

 採用された回答

Ive J
Ive J 2021 年 8 月 27 日
編集済み: Ive J 2021 年 8 月 27 日

0 投票

This might work
tab = table({'Car'; 'Bike'; 'Cycle'}, [50; 20; 5], [2; 1; 0],...
'VariableNames', {'Vehicle', 'speed', 'fuel'})
tab = 3×3 table
Vehicle speed fuel _________ _____ ____ {'Car' } 50 2 {'Bike' } 20 1 {'Cycle'} 5 0
filterIn = 'Bike'; %input('what to filter? ', 's');
idx = ismember(tab.Vehicle, filterIn);
if ~any(idx)
fprintf('oops, not found!\n')
return
end
dataTable = tab(idx, :);
dataStruct = table2struct(dataTable, 'ToScalar', true);
as table:
disp(dataTable)
Vehicle speed fuel ________ _____ ____ {'Bike'} 20 1
or struct:
disp(dataStruct)
Vehicle: {'Bike'} speed: 20 fuel: 1

2 件のコメント

Dhananjay Singh
Dhananjay Singh 2021 年 8 月 27 日
after getting this is there a way we can assign say x = speed i.e x = 20. x is already defined
Thank you for this.
Ive J
Ive J 2021 年 8 月 27 日
編集済み: Ive J 2021 年 8 月 27 日
Yes you can easily assign struct fields to variables, but it's better to use structs as they are, it's much easier and more clear!
x = dataStruct.speed; % or other field names, see fieldnames help

サインインしてコメントする。

その他の回答 (1 件)

Campion Loong
Campion Loong 2021 年 8 月 30 日

1 投票

Picking up from @Ive J's example, but use 'Vehicle' as Row Labels to take advantage of native table indexing:
t = table([50; 20; 5], [2; 1; 0], 'VariableNames', ["speed", "fuel"], 'RowNames', ["Car"; "Bike"; "Cycle"])
t = 3×2 table
speed fuel _____ ____ Car 50 2 Bike 20 1 Cycle 5 0
t("Bike", ["speed", "fuel"]) % or if you want all variables, t("Bike", :)
ans = 1×2 table
speed fuel _____ ____ Bike 20 1

カテゴリ

ヘルプ センター および File ExchangeTables についてさらに検索

質問済み:

2021 年 8 月 27 日

回答済み:

2021 年 8 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by