How can I link user inputs to corresponding cell in table
1 次查看(过去 30 天)
显示 更早的评论
Hi, I've attached an example of the excel file I will be using. so far, I have linked the user inputs and matched them to the corresponding cells in the table. Now I want matlab to be able to use those linked cells and output the separation distance from the column next to the pipe columns. I've attached what I have in my code but I've omitted the section where it imports the excel file into tables. ideally, I'd like to extend this into multiple user inputs and have matlab calculate the required separations but thats a question for another time :D. Any help will be greatly appreciated.
Thanks
%Ask user to select pipe types
pipe1prompt = 'Select the first pipe type ';
pipe1 = input(pipe1prompt, 's')
SegregationSpacingSimplified.Pipe_1==pipe1;
pipe2prompt = 'select the second pipe type ';
pipe2 = input(pipe2prompt, 's')
SegregationSpacingSimplified.Pipe_2==pipe2;
0 个评论
回答(1 个)
Ishaan Mehta
2024-2-13
Hi Zain,
I understand that you have a spreadsheet with values for "separation" for the given pairs of "Pipe1" and "Pipe2", and you wish to write a MATLAB function that can ideally accept the values for "Pipe1" and "Pipe2" to return the corresponding "separation" value.
We can use "readtable" to read the spreadsheet data into MATLAB, then some required table indexing operations to match the provided inputs with values in the table, and return the required output, as depicted below.
getSeparation("AB4A", "P2")
getSeparation("ab6", "t1")
getSeparation("AB5", "P4") % should print a warning as no match will be found in the data
The following code block for function "getSeparation" should help you implement the same.
function separationValue = getSeparation(p1, p2)
p1 = upper(p1);
p2 = upper(p2);
data = readtable("Example.xlsx");
data = data(:, {'Pipe1', 'Pipe2', 'Separation'});
% convert columns from cell arrays to strings
data.Pipe1 = string(data.Pipe1);
data.Pipe2 = string(data.Pipe2);
% remove empty rows if any
data(data.Pipe1 == "", :) = [];
% Find the row index where both Pipe1 and Pipe2 match the given names
rowIndex = find(data.Pipe1 == p1 & data.Pipe2 == p2);
% Check if a matching row was found
if isempty(rowIndex)
warning('No matching row found for the given pipe names.');
separationValue = NaN; % Return NaN or some error code if no match is found
else
% Extract the Separation value from the found row
separationValue = data.Separation(rowIndex);
end
end
This uses the "find" function to find a non-zero value which would indicate the row index of the row with the matching values. Find more information about the "find" function in the following documentation page:
Hope it helps.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!