Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How do I load a .dat file in a different directory?

Subject: How do I load a .dat file in a different directory?

From: John Wong

Date: 18 Feb, 2012 00:00:17

Message: 1 of 3

% File: some_script.m

load('data_src/MY_BINARY.dat'); % read data into MY_BINARY matrix
month = MY_BINARY(:,1); % copy first column of MY_BINARY into month
precip = MY_BINARY(:,2); % and second column into PRECIP

But this said
{Undefined variable MY_BINARY. } {Undefined variable MY_BINARY. } {Error using month (line 37) Please enter D. } 

How do I load data file that is not in the same directory as my dot m file?
I tried ../data_src , etc

Thank you.

Subject: How do I load a .dat file in a different directory?

From: K

Date: 18 Feb, 2012 21:56:12

Message: 2 of 3

"John Wong" wrote in message <jhmpmh$g5k$1@newscl01ah.mathworks.com>...
> % File: some_script.m
>
> load('data_src/MY_BINARY.dat'); % read data into MY_BINARY matrix
> month = MY_BINARY(:,1); % copy first column of MY_BINARY into month
> precip = MY_BINARY(:,2); % and second column into PRECIP
>
> But this said
> {Undefined variable MY_BINARY. } {Undefined variable MY_BINARY. } {Error using month (line 37) Please enter D. } 
>
> How do I load data file that is not in the same directory as my dot m file?
> I tried ../data_src , etc
>
> Thank you.

You either provide the full static path (i.e. 'c:/MyFolder/../MyDatFile.dat') or use dynamic reference using ../ or ./.
Where exactly does your data_src lbrary located relative to your root folder?
K

Subject: How do I load a .dat file in a different directory?

From: ImageAnalyst

Date: 19 Feb, 2012 02:29:39

Message: 3 of 3

Try this more robust code.

fullFileName = fullfile(folder, 'MY_BINARY.DAT');
if exist(fullFileName, 'file')
  % File does exist. Load values into a structure, assuming it's a
mat format file.
  recalledStructure = load(fullFIleName);
  % See if this variable is even in the file.
  hasField = isfield(recalledStructure , 'MY_BINARY');
  if hasField
    % Variable does exist inside the file.
    % Transfer columns to other variables.
    month = recalledStructure .MY_BINARY(:,1); % copy first
column of MY_BINARY into month
    precip = recalledStructure .MY_BINARY(:,2); % and second
column into PRECIP
  else
    % Variable is not in the MY_BINARY.DAT file. Alert user.
    errorMessage = sprintf('MY_BINARY.DAT does not exist in file:\n.',
fullFileName);
    uiwait(warndlg(errorMessage));
  end

else
  % File does not exist.
  errorMessage = sprintf('%s does not exist.', fullFileName);
  uiwait(warndlg(errorMessage));
end

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us