How to load data from a location other than current directory

조회 수: 52 (최근 30일)
Anthony Barone
Anthony Barone 2015년 7월 13일
답변: PAVANRAJ PRAJJU 2022년 5월 15일
I would like to be able to load a .mat file from an arbitrary location that is not my currently selected folder.
Example: data is stored in C:\users\me\Documents\MATLAB\data\example_data.mat
current selected folder (where script is running) is C:\users\me\Documents\MATLAB\projects\project1\example_code.m
What code could I use to load example_data.mat in a some script (example_code.m) running from any arbitrary folder in MATLAB?
Thanks!

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 13일
편집: Azzi Abdelmalek 2015년 7월 13일
data=load('C:\users\me\Documents\MATLAB\data\example_data.mat')
data is a struct variable, for example:
data=
a: [1 2 3]
b: [2 3 4]
c: [3 4 5]
The you can access all your variables
a=data.a
b=data.b
c=data.c
  댓글 수: 2
Anthony Barone
Anthony Barone 2015년 7월 13일
Thanks!
I could have sworn I tried just that before and it didn't work....maybe I misused the apostrophes
Azzi Abdelmalek
Azzi Abdelmalek 2015년 7월 13일
편집: Azzi Abdelmalek 2015년 7월 13일
If you want to assign values to each variable automatically:
filename='C:\users\me\Documents\MATLAB\data\example_data.mat'
str=whos('-file',filename)
str={str.name}
load(filename,str{:})

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

추가 답변 (1개)

PAVANRAJ PRAJJU
PAVANRAJ PRAJJU 2022년 5월 15일
What is the command that is used to call data from a file which is saved in the same directory

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by