How can I link user inputs to corresponding cell in table

1 visualización (últimos 30 días)
Zain Zia
Zain Zia el 30 de Nov. de 2021
Respondida: Ishaan Mehta el 13 de Feb. de 2024
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;

Respuestas (1)

Ishaan Mehta
Ishaan Mehta el 13 de Feb. de 2024
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")
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
ans = 300
getSeparation("ab6", "t1")
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
ans = 250
getSeparation("AB5", "P4") % should print a warning as no match will be found in the data
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
Warning: No matching row found for the given pipe names.
ans = NaN
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.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by