Compare text to a column value and write corresponding row to new file

I need to write a code that will read every value in a matrix colum (about 5,000 entries long), compare the value in each cell to an existing variable and if the cell value matches the existing variable, write the entire row to a new table.
I am thinking I will need to use a 'for' loop to evaluate the entire matrix and string compare but I am not sure how to go about writing this.

 Akzeptierte Antwort

You can find the indices of matching values in matrix and cell.
matrix=randi(10,1,100);
cell_existing_variable=num2cell(randi(10,1,100),1);
find(matrix==cell2mat(cell_existing_variable))
If this helps you, its good. Otherwise, I would request you to attach mat file having sufficient varibales.

8 Kommentare

Thank you. I've attached the file I am working with. What I am trying to do is place every row that corresponds to the aircraft model 'Cessna' into a new table that I can then write into a new excel sheet.
I have what I have written so far below
%read Data
data = xlsread('Q1data_aircraft.xls');
size(data);
model = 'Cessna';
A = 2;
for i = 1:length(data)
model2 = raw(A:2);
tf = strcmp(model,model2);
if tf = 1
Index = %this is the part where I am not sure what to do
%I need to get the index of the first cell containing
%Cessna, extract the entire row to another table and move
%onto the next cell that contains Cessna
end
end
clc
clear
[~, ~, Q1dataaircraft] = xlsread('Q1data_aircraft.xls','Q1data_aircraft','A2:C16384');
Q1dataaircraft(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),Q1dataaircraft)) = {''};
model = 'cessna';
Q1dataaircraft_model=Q1dataaircraft(strcmp(Q1dataaircraft(:,2),model),:);
Q1dataaircraft_model_table=table(Q1dataaircraft_model);
xlswrite('Q1dataaircraft_model',Q1dataaircraft_model)
Hope this helps.
Thank you. I ran the code and got this error, not sure what I need to change
Warning: Range cannot be used in 'basic' mode. The entire sheet will be loaded.
> In xlsread (line 208)
In Read_aircraft_data (line 5)
Warning: Unable to write to Excel format, attempting to write file to csv format. To write to an Excel file, convert your data to a table and use writetable.
> In xlswrite (line 179)
In Read_aircraft_data (line 10)
Error using xlswrite (line 192)
An error occurred on data export in CSV format.
Error in Read_aircraft_data (line 10)
xlswrite('Q1dataaircraft_model',Q1dataaircraft_model)
Caused by:
Error using dlmwrite (line 104)
The input cell array cannot be converted to a matrix.
ANKUR KUMAR
ANKUR KUMAR am 16 Mär. 2021
Bearbeitet: ANKUR KUMAR am 16 Mär. 2021
Are you using exactly the same code which I have shared, or have you made some changes?
Could you please confirm whether Q1dataaircraft_model is getting calculated or not?
I am using the code exactly as is. Q1dataaircraft_model is being calculated. I checked and it is producing a table correctly so the code works but for some reason isn't writing to excel
You can use dlmwrite or dlmcell to write into a text file.
Which version of MATLAB are you using?
Okay, thank you. I am using the online app
I doubt whether xlswrite works in online app or not. xlswrite is working well in MATLAB2017b (which I am using). @MathWorks Support Team

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by