.mat files not loading correctly
조회 수: 13 (최근 30일)
이전 댓글 표시
Hi all,
I am very new to MATLAB, and the first program i wrote which loaded a file loaded a file named data_vector. Now, whenever I load any file it gets automatically stored in a variable of name data_vector. This means that the following code will not compile on my copy of MATLAB R2016b.
clear
load test_data;
plot(test_data(1,:)) %test_data is a 1000x500 2D array
Please help!
Edit: the specific error message is
Reference to a cleared variable test_data.
Error in phase_detector (line 4)
plot(test_data(1,:))
댓글 수: 7
dpb
2017년 4월 14일
편집: dpb
2017년 4월 16일
This doesn't save them under different variable names, only as a different file; that's the point. SAVE/LOAD is primarily a shorthand for precisely that purpose; save/retrieve a given variable or set thereof for quickly being able to reproduce a snapshot of a previous session or the like without having to worry about extraneous code.
If you really want to save a set of data but read it back in a more general sense, use something other than SAVE (like fwrite/fread for unformatted) that doesn't include such additional information with it.
Or, as Steven notes, if you want to force rename a variable from a .mat file, there's always the structure route albeit it is a fair amount of overhead often compared to using simple arrays.
채택된 답변
Steven Lord
2017년 4월 14일
- Calling clear to clear all variables is rarely necessary in a function.
- If you call load with an output argument, the output argument will be a struct array and the variables that were in the MAT-file will be read into fields in that output argument. This allows you to avoid conflicts with variables that already existed in the workspace. I recommend you use this approach; that way the name of the variable you have to access to use your data is predictable. You can use dynamic field names in conjunction with fieldnames if you don't know the names of the variables in your MAT-file but need to iterate over them.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!