Hi,
I am trying to extract data from a sequence of files. I also need to extract the value of x that corresponds to peak y value from each plot. I have tried the following code but I can't get results. I would be grateful if someone can check it for me. I have attached herewith the files for your kind reference.
close all; clear all; clc;
folder = cd;
for k = 1:2;
for j = 1:2;
matFilenameA = sprintf('X%d.out', k);
matData1 = load(fullfile(cd, matFilenameA));
%
matFilenameB = sprintf('Y%d.out', j);
matData2 = load(fullfile(cd, matFilenameB));
x = matFilenameA(:,2);
y = matFilenameB(:,2);
[maxvaly,idx] = max(y) ;
maxvalx = x(idx);
fid=fclose('all');
end
end

댓글 수: 4

Birdman
Birdman 2017년 11월 11일
In which part of code do you have trouble?
First, the loop only calculates one set. Second, I think the following part has a problem.
Thank you.
x= matFilenameA(:,2);
y= matFilenameB(:,2);
[maxvaly,idx] = max(y) ;
maxvalx = x(idx);
fid=fclose('all');
end
end
You do not assign numerical arrays to x and y. I think you should write
x= matData1(:,2);
y= matData2(:,2);
Thanks. I have tried it, but the it still shows the following error message:
Index exceeds matrix dimensions.
Error in Try2 (line 14)
maxvalx = x(idx);

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

 채택된 답변

KSSV
KSSV 2017년 11월 11일

0 개 추천

files = dir('*.txt') ;
N = length(files) ;
iwant = zeros(N,2) ;
for i = 1:N
data = importdata(files(i).name) ;
[val,idx] = max(data(:,2)) ;
iwant(i,:) = [data(idx,1),val] ;
end
Copy all the text file sin one folder and runt his code.

댓글 수: 5

Ismail Qeshta
Ismail Qeshta 2017년 11월 11일
Thanks KSSV. I actually need to plot Y values against X. Your code does not do that.
KSSV
KSSV 2017년 11월 11일
편집: KSSV 2017년 11월 11일
files = dir('*.txt') ;
N = length(files) ;
figure
hold on
for i = 1:N
data = importdata(files(i).name) ;
plot(data(:,1),data(:,2))
end
Ismail Qeshta
Ismail Qeshta 2017년 11월 11일
Thanks KSSV. Can we extract the value of X that corresponds to the maximum value of y? I need to keep the loop running and extracting the values for all N sets.
KSSV
KSSV 2017년 11월 11일
That's what the first code does.....you asked the same question yesterday....without reading your question I answered that.
Ismail Qeshta
Ismail Qeshta 2017년 11월 11일
Thank you KSSV for your great help yesterday and today. Yes, I actually could get the the code yesterday work for only one set of data. But I still have confusion about how to get it worked in a loop.

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

추가 답변 (0개)

카테고리

질문:

2017년 11월 11일

댓글:

2017년 11월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by