이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How to read in a file automatically through loop?
조회 수: 5 (최근 30일)
이전 댓글 표시
Hello everyone!
I need your help in finding a way to read in a a file each iteration.
My Folder looks like that:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/235038/image.png)
and I want that each time it reads the file in order.
For example, for j=1--> reads in the first file
for j=2--> reads in the second file
.....and so on.
Until now, I used to read in the files every time manually,but I would like if my script could do it on its own to save time.
I appreciate any help :)
댓글 수: 12
Ted Shultz
2019년 8월 22일
편집: Ted Shultz
2019년 8월 22일
fileList = ls ('*.mat')
you can then loop over the list you get.
Walter Roberson
2019년 8월 22일
You might want "natural file sort" from the File Exchange if you have more than 10 files, as otherwise Marker10 can end up being listed right after Marker1
Ifigenia Aslanidou
2019년 8월 23일
Hey,
Thanks a lot for your answers!
I forgot to mention , that my measurements are in a subfolder in my original folder(where all the other scripts are).
So after listing my .mat files I get a char variable. How do I go on?I am not really experienced in Matlab :/
@Walter Roberson: I don't think I will exceed the 10 files, so I guess that won't be a problem.
Stephen23
2019년 8월 23일
@Ifigenia Aslanidou : Note that ls is intended for displaying in the command window, it is not very practical for using within code. For processing filenames using code you should use dir, exactly as the MATLAB documenation recommends (check the examples):
You might like to check the many examples here:
Ifigenia Aslanidou
2019년 8월 23일
Hello!Thanks for you answer! So thats what I wrote:
path = 'C:\Users\aslan\OneDrive\Desktop\Studienarbeit 2019\Messungen'; %my path
measureFiles = dir(fullfile(path,'\*.mat')); %Putting all files together in a variable
numfiles = length(measureFiles);
mydata = cell(1, numfiles);
mydata{j} = read(measureFiles(j));
But the 'read ' line is wrong.
MeasureFiles is a struct. How do I read in the files from it?
Stephen23
2019년 8월 23일
편집: Stephen23
2019년 8월 23일
"How do I read in the files from it?"
As the dir documentation explains, that structure array contains the filenames (and other file meta-data), you can access the filenames using the name field, e.g. the k-th name is:
S(k).name
You will need to add the subfolder to that filename using fullfile. To actually import the file data you will need to pick a suitable function (for .mat files load is the function to use):
P = 'C:\Users\aslan\OneDrive\Desktop\Studienarbeit 2019\Messungen'; % Do NOT use "path" !
S = dir(fullfile(path,'*.mat')); % do NOT write the separator.
for k = 1:numel(S)
F = fullfile(P,S(k).name);
T = load(F);
... do whatever with the structure T
end
See also:
Ifigenia Aslanidou
2019년 8월 23일
Thanks!
The measurements are loading,but the Array that is in it wont be read in :/
![measurement.PNG](https://www.mathworks.com/matlabcentral/answers/uploaded_files/235110/measurement.png)
I get an error that says 'Undefined function or variable 'OUTPUT' '
Stephen23
2019년 8월 23일
편집: Stephen23
2019년 8월 23일
"I get an error that says 'Undefined function or variable 'OUTPUT' '"
Without seeing your code I have no idea what you are doing. Possibly you need to access the output field of the structure that you load-ed.
T = load(...);
OUTPUT = T.OUTPUT;
If that does not resolve your issue, then you need to show the exact code that you are using, and also give the complete error message. That means all of the red text.
Walter Roberson
2019년 8월 23일
Also show the result of
whos('-file', 'C:\Users\aslan\OneDrive\Desktop\Studienarbeit 2019\Messungen\201804_A81)001_Marker1_30s_16*_943hPa_pp.mat')
(repair the file name if I made a typing mistake; the font is somewhat small and I am not certain I got all of the characters correct.)
Ifigenia Aslanidou
2019년 8월 25일
The OUTPUT=T.OUTPUT doesnt work.. It runs the first loop and then an error comes :
''Dot indexing is not supported for variables of this type.
Error in Inputs_I_Lp_IfigeniaAslanidou (line 13)
F = fullfile(P,S(j).name); ''
My code looks like that:
clear all;
%% Pre-allocate the variables that will save ALL of the data
Preallocating_variables_save_all_data;
P = 'C:\Users\aslan\OneDrive\Desktop\Studienarbeit 2019\Messungen';
S = dir(fullfile(P,'*.mat'));
for j=1:numel(S)
%------------------------------INPUTS---------------------------------%
% Datei einlesen (Array "OUTPUT")
F = fullfile(P,S(j).name);
T = load(F);
OUTPUT = T.OUTPUT;
......%some measurements and plots
end
After using whos I get this :
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/235240/image.png)
Ifigenia Aslanidou
2019년 8월 25일
I realised whichmight be causing the trouble: the S returns a Matrix : N x 6 instead of the measurements list..(struct)
Ifigenia Aslanidou
2019년 8월 25일
Ok now it worked!
Script:
clear all;
%% Pre-allocate the variables that will save ALL of the data
Preallocating_variables_save_all_data;
P = 'C:\Users\aslan\OneDrive\Desktop\Studienarbeit 2019\Messungen';
D = dir(fullfile('Messungen','*.mat')); %I dont know what the problem was because with S... instead of D it caused the troubles, when I ran it was a matrix,when I typed it it in command it... was a list
for j=1:6
%------------------------------INPUTS---------------------------------%
% Datei einlesen (Array "OUTPUT")
F = fullfile('Messungen',D(j).name); %replaced P with 'Messungen' (how the meas. file is... named after
T = load(F);
OUTPUT = T.OUTPUT;
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)