0

Here's a part of my code where I am entering a name of the .mat file, which is located in the same folder as my code. However it does not identify the file name and gives an error:

"??? Error using ==> load

Unable to read file 'q.mat': No such file or directory."

q_type=input('Do you want to use q from "A", "B" or from a saved .mat file? Enter the exact name: ','s');
q_type=mat2str([q_type'.mat'])
load(q_type)

However if I use the load command in the command window directly as follow, then it gives no error and loads the file:

load('q_A.mat')

Why is it doing like this?

  • 2
    remove the MAT2STR function call – Amro Oct 24 '10 at 04:07
  • you are using file name without its path, if it is not in the MATLAB path then MATLAB can not find it. Use function fullfile(). – Mikhail Poda Oct 24 '10 at 07:45
  • 1
    @Harpreet: instead of the last two lines, use: `load( [q_MethodType '.mat'] )`. Also I posted a more elegant solution below... – Amro Oct 24 '10 at 18:47
  • I still do not understand why using mat2str()? For chars it should behave like an identity function x=f(x). You also you do not need quotes in the file name when it is used programmatically. Strings are displayed with quotes but do not have them as additional elements of string array. The error you get without mat2str is really weird. – Mikhail Poda Oct 24 '10 at 19:21

1 Answers1

3

Here's a more user-friendly solution using a modal dialog:

[fileName pathName] = uigetfile({'*.mat' 'MAT-files (*.mat)'}, 'Load Data', '.');
if pathName == 0, error('No file selected'), end
load( fullfile(pathName,fileName) )

feel free to customize it as needed.

Amro
  • 123,847
  • 25
  • 243
  • 454