How do I use inputs to find and manipulate data in a table?

19 ビュー (過去 30 日間)
Ryan Adams
Ryan Adams 2020 年 7 月 22 日
回答済み: Rajani Mishra 2020 年 7 月 29 日
Hello,
I am working on a project where I have a table that shows data for different soups. In the table I have columns for the name, calories, cups, protein, carbs, etc.. and the rows are the different soups (i.e. tomato, chicken noodle, etc..). What I want to do is use two input functions to give an array of values for a specific row and the various columns. The first input would prompt the user to give the name of the soup. The second input would prompt the user to tell how many cups of the soup. What I'm trying to figure out is how to use both inputs together in a way where the first input would find the specific row (by searching the name), and the second input would take the values from that row and multiply them by the difference in number of cups. So that the final output would give data for that soup at a certain amount of cups. Any examples of code would be greatly appreciated!
Here's two images that better illustrate my question by using the same concept but with cereal instead of soup.
Thanks!

回答 (1 件)

Rajani Mishra
Rajani Mishra 2020 年 7 月 29 日
The two steps asked are:
  • Find a row from a table using the first input
  • Multiply the row received after first operation according to the second input.
I have created a table for above operations, example table is taken from this link.
load patients
T = table(Age,Height,Weight,...
'RowNames',LastName);
% Table created
For first step:
name = 'Williams' %Here I have stored row name dirctly in a variable,
% you can use the variable containing user input
T2 = T(name,:)
Second step:
result = table2array(T2); % I have transformed the row received to a vector for
% multiplying it by a specific number
result = result * factor
%factor is multiplication variable calculated according to the second input

カテゴリ

Help Center および File ExchangeTables についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by