Azzera filtri
Azzera filtri

Find occurrence of a character/number combination input in a Seperate array

3 visualizzazioni (ultimi 30 giorni)
in the code, I am trying to ask the user for multiple inputs which is placed into a cell array, than I want to use that array (which has the user inputs) to search the indices of occurence in another cell array, which was an extration of an excel file that (has a title in every 5 columns that the user input is indicative of). Once I locate that indices I plan on usng the values under that column till the next title to do calculations.
for i = 1:1:(width(Engtype)*width(TCValFresh)*TransType)
prompt = {'Add a New Combination?'};
dlgtitle = 'Combination Prompter';
dims = [1 35];
definput = {'No'};
x = inputdlg(prompt,dlgtitle,dims,definput);
no= {'no','NO','No'};
if contains(x,no) == 1
break
else
prompt = {'Enter Engine ID','Enter TC ID','Enter Transmission ID'};
dlgtitle = 'Combination';
dims = [1 80];
definput = {'C4.4 DITAAC 74kW','Sachs W300_306','Cool Transmission'};
ReqCombo = inputdlg(prompt,dlgtitle,dims,definput);
ReqUse = transpose(ReqCombo);
r = r+1;
for c = 1:1:width(ComboStacker)
ComboStacker(r,c) = ReqUse(1,c);
end
end
end

Risposte (1)

Suraj Kumar
Suraj Kumar il 23 Mag 2024
Hi Alp,
To find the occurrence of a character or number combination input in a separate array, you can follow these steps.
1. You can store the input in a cell array and then compare it against the data extracted from excel sheet. Now to get data from the user you can use the input dialog box in MATLAB and store the data in a cell array.
You can go through the code below for better understanding:
r = 0; % Row counter for ComboStacker
ComboStacker = {}; %Empty cell to store the user inputs
maxIterations = 5;
for i = 1:maxIterations
prompt = {'Add a New Combination?'};
dlgtitle = 'Combination Prompter';
dims = [1 35];
definput = {'No'};
x = inputdlg(prompt, dlgtitle, dims, definput);
% Check if user inputs more data
no = {'no', 'NO', 'No'};
if any(contains(x, no))
break;
else
% Prompt for new combination details
prompt = {'Enter Engine ID', 'Enter TC ID', 'Enter Transmission ID'};
dlgtitle = 'Combination';
dims = [1 80];
definput = {'C4.4 DITAAC 74kW', 'Sachs W300_306', 'Cool Transmission'};
ReqCombo = inputdlg(prompt, dlgtitle, dims, definput);
% Incrementing the counter and storing the data in the array
r = r + 1;
ComboStacker(r, :) = ReqCombo';
end
end
2. Now to use these stored values and match against the data extracted from an excel sheet you can use readtablefunction to extract the data from the excel sheet and “cellfun function to compare the values from the stored cell array with the extracted data from excel sheet.
You can refer the below code for better understanding:
% Read data from excel file using readtable
filename = 'data.xlsx';
dataTable = readtable(filename, 'ReadVariableNames', false);
data = table2cell(dataTable);
% Initializing an array to store the indices of found combinations
matchingIndices = {};
% Iterate through each value in ComboStacker and search in the table
for i = 1:size(ComboStacker, 1)
for j = 1:size(ComboStacker, 2)
userInput = ComboStacker{i, j};
matches = cellfun(@(x) strcmp(x, userInput), data);
indices = find(matches);
% If non empty store the indices
if ~isempty(indices)
matchingIndices{end+1} = indices;
end
end
end
You can filter out the redundant indices by using the unique function.
To know more about the ‘’readtable or cellfun functions, you can refer the following links:
Hope this helps!

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by