Using the find function in a loop

조회 수: 1 (최근 30일)
Erik Verdijk
Erik Verdijk 2018년 4월 9일
댓글: Erik Verdijk 2018년 4월 9일
I wrote this script to import multiple files. Now I try tofind the variables. Every file has a different amount of rows. I like to use the find function to find my variables. Every time I get an error. Can someone help me?
  댓글 수: 1
KSSV
KSSV 2018년 4월 9일
What error you get? Which line you are getting the error? What file we have to select to run the code?

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

채택된 답변

Guillaume
Guillaume 2018년 4월 9일
The immediate cause for the error is simple, you already have a variable called datarecords and it's not a cell array. Since none of the variables you're using in your loop are predeclared you're either reusing whatever exists or creating them in the first step of the loop.
datarecords = {};
mydata = {};
before the loop would fix that error.
However the rest of the code doesn't make much sense. I very much doubt that importdata is going to return a structure when loading a text file, so your mydata{k}.textdata is probably an error as well.
k is either 1 or 4, never any other value, so it's unclear why it's used as an index. And if its only purpose is to break out of the loop then:
while true
%...
if strcmp(Answer, 'No')
break;
end
end
is better.
I think that rather than fixing that script, you'd be better off explaining better what you want to do and showing an example of the text file you want to load and telling us which part of that file you're interested in.
If you are loading text files, why don't you use readtable?
  댓글 수: 2
Guillaume
Guillaume 2018년 4월 9일
편집: Guillaume 2018년 4월 9일
Ok, now that you've explained better what you're trying to do, here's how I'd do it: First, read the whole file as text and find on which line is 'DATA RECORDING' then read it again using readtable giving it the correct number of 'HeaderLines':
[FileName,PathName] = uigetfile('../*.txt','MultiSelect','off','Load log file','C:\Users\');
fullname = fullfile(PathName, FileName); %fullfile is safer than strcat
%find the line where DATA RECORDING is in file.
filecontent = fileread(fullname); %read whole file
filelines = strsplit(filecontent, '\n'); %split into lines
dataline = find(startsWith(filelines, 'DATA RECORDING'));
assert(numel(dataline) == 1, 'DATA RECORDING not found or found more than once');
%now read the file skipping header
data = readtable(fullname, 'HeaderLines', dataline);
Erik Verdijk
Erik Verdijk 2018년 4월 9일
thank you so much

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

추가 답변 (1개)

Erik Verdijk
Erik Verdijk 2018년 4월 9일
I have files that i want to import. Each of these files has a unknown number of header lines. I like to find that amount. The usefull data (The data I want to plot) start 2 lines after the words data record. I tried to find the row of DATA RECORD: by using datarecords{k} = find(cellfun(@strcmp,mydata{k}.textdata,repmat({'A'},size(mydata{k}.textdata)))==1); This code works without the loop. I attached an example of a file that I should like to import. I have several of them. If you look to line (I guess) 79 you see data recording 2 lines later, the data start.
  댓글 수: 1
Guillaume
Guillaume 2018년 4월 9일
Please use comments rather than starting a new answer.

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

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by