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

조회 수: 2(최근 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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by