# read special cell in excel file

조회 수: 1(최근 30일)
MOzhdeh Salimi 2021년 9월 8일
댓글: Mathieu NOE 2021년 9월 8일
Hi all
This is my code. The first column in my file data is txt. Now I want when raw=2 equal raw=3 to write my station name that put in raw=1.
a=('sabet.xlsx');
c1=raw(:,1);
c2=raw(:,2);
c3=raw(:,3);
for b=1:1:numel(c2)
for c=1:1:numel(c3)
for d=1:1:numel(c1)
if b == c
end
end
end
end
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
MOzhdeh Salimi 2021년 9월 8일
Hi Dear
I attached my file as txt file

댓글을 달려면 로그인하십시오.

### 채택된 답변

Mathieu NOE 2021년 9월 8일
hello again
so here a small code to extract the lines where the 2 numbers in row 2 and row 3 are below a given threshold. FYI I could not find any line where the two numbers are strictly equal but there are a few ones very close. So you can decide about the threshold (limit).
here we have 3 selected names with threshold = 10 ;
% 'Anar' 40839 40830
% 'Arak' 40769 40761
% 'Baneh' 99280 99281
code is as follows : (you can expand it to save the result in an excel sheet for example or in a text file)
clc
clearvars
C = table2cell(T);
[m,n] = size(C);
% init
C2 = cell2num(C(:,2));
C3 = cell2num(C(:,3));
index = [];
delta = [];
threshold = 10;
% main loop
for ci = 1:m
if ~isnan(C2(ci)) && ~isnan(C3(ci))
tmp = abs(C2(ci) - C3(ci)); % compute abs delta between two values (raw 2 and raw3 )
if tmp <= threshold % keep line index of raws with delta below threshold
index = [index; ci]; % agregate index
delta = [delta; tmp];% agregate delta value
end
end
end
% plot(index,delta);
celldisp(C(index,1));
% 'Anar' 40839 40830
% 'Arak' 40769 40761
% 'Baneh' 99280 99281
##### 댓글 수: 8표시숨기기 이전 댓글 수: 7
Mathieu NOE 2021년 9월 8일
sorry , I misunderstood the question before. now my brain has re-started to work !
check this one !
clc
clearvars
C = table2cell(T);
[m,n] = size(C);
% init
% C2 = cell2num(C(:,2)); % release above R2018 ?
% C3 = cell2num(C(:,3)); % release above R2018 ?
C2 = another_cell2num(C(:,2)); % release below R2018
C3 = another_cell2num(C(:,3)); % release below R2018
index1 = [];
index2 = [];
delta = [];
threshold = 10;
% main loop
for ci = 1:m
if ~isnan(C2(ci)) && ~isnan(C3(ci))
%tmp = abs(C2(ci) - C3(ci)); % compute abs delta between two values (raw 2 and raw3 )
[val , ind] = min(abs(C2(ci) - C3));
if val < 1 % keep line index of raws with delta below threshold
index1 = [index1; ci]; % agregate index
index2 = [index2; ind]; % agregate index
delta = [delta; val];% agregate delta value
end
end
end
% plot(index,delta);
% display in command window :
% first station code / corresponding (second) col value / second station code / corresponding (third) col value /
[C(index1,1) C(index1,2) C(index2,1) C(index2,3)]
% ans =
%
% 18×4 cell array
%
% {''Abali'' } {} {''Bukan'' } {}
% {''Abarkuh'' } {} {''Arak (Airport)'' } {}
% {''Ahar'' } {} {''Darrehshahr'' } {}
% {''Arak'' } {} {''Bushehr (Airport)''} {}
% {''Arsanjan'' } {} {''Dorud'' } {}
% {''Bam'' } {} {''Dehagh'' } {}
% {''Bandarabbas'' } {} {''Bushehr (Coastal)''} {}
% {''Bandar-E- Mahshahr''} {} {''Bandar-E-Deylam'' } {}
% {''Bandar-E-Khamir'' } {} {''Ahvaz'' } {}
% {''Bandar-E-Lengeh'' } {} {''Bandar-E-Dayyer'' } {}
% {''Biyarjomand'' } {} {''Eqlid'' } {}
% {''Damghan'' } {} {''Arak'' } {}
% {''Daregaz'' } {} {''Ajabshir'' } {}
% {''Dehloran'' } {} {''Abumusa Island'' } {}
% {''Dorud'' } {} {''Chahbahar'' } {}
% {''Dowshan Tappeh'' } {} {''Daran'' } {}
% {''Emamzadeh Jafar'' } {} {''Arsanjan'' } {}
% {''Eyvan'' } {} {''Bandar-E-Lengeh'' } {}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [outputmat]=another_cell2num(inputcell)
% Function to convert an all numeric cell array to a double precision array
% ********************************************
% Usage: outputmatrix=cell2num(inputcellarray)
% ********************************************
% Output matrix will have the same dimensions as the input cell array
% Non-numeric cell contest will become NaN outputs in outputmat
% This function only works for 1-2 dimensional cell arrays
if ~iscell(inputcell), error('Input cell array is not.'); end
outputmat=zeros(size(inputcell));
for c=1:size(inputcell,2)
for r=1:size(inputcell,1)
% original code
% if isnumeric(inputcell{r,c})
% outputmat(r,c)=inputcell{r,c};
% else
% outputmat(r,c)=NaN;
% end
%Works great if you use addition by C Schwalm to the if statement. If statement within the code should now look like :
if isnumeric(inputcell{r,c})
outputmat(r,c)=inputcell{r,c};
else
outputmat(r,c)=NaN;
end
end
end
end

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!