save and load command give error

Dear,
I'm trying to use these functions but it always give me error. I have a matrix and I need to save it then call it from another file, I used in my version R2012b in my desktop these command and they work but not in R2013a in my laptop.
save('Afile.txt','A','-ascii');
A=load('Afile.txt');
So I used
savefile = 'Afile.mat')
save(savefile,'A');
like this example how not work also in R2013a and its from documentation.
savefile = 'pqfile.mat';
p = rand(1, 10);
q = ones(10);
save(savefile, 'p', 'q');
I got this message
error using save
( unable to write file pqfile.mat: permission denied)
I know this mean the matrix is not read by the directory so what should I do to use save and load command.
thanks for any help.
Regards,
Nadia Nadi,

2 Comments

The error is due to the fact that the root folder is the one that is selected by default is on the local disk C :, that is why it leaves error of not having write permission. You just have to change the root folder where it says current folder.
The root folder is not the one that is selected by default on C: . As far as I know, it never has been in the history of MATLAB -- but I can't be sure about versions before MATLAB 5.0
In recent versions, the default when you start MATLAB is to be put into the directory that you were in at the time you exited MATLAB last. A more detailed description is given at https://www.mathworks.com/help/matlab/matlab_env/matlab-startup-folder.html
The first time you start MATLAB on Windows, MATLAB will try to create Documents\MATLAB for you.
In sufficiently old versions, or if MATLAB is not able to create the MATLAB folder for you, your initial directory might be the one in which MATLAB is installed.

Sign in to comment.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 30 Jul 2015
Change the folder where you want to save your file. Choose for example documents

11 Comments

Hello Azzi,
thanks for answering me, but I didn't understood what you mean.
could you please explain more with an example.
regards,
nadai
Type
pwd
To get your current folder, it's probably ic c:\Programme Files, Windows 7 doesn't allow writing in this folder. Just change the folder where you save your file
Dear Azzi,
I wrote pwd and the result as you said C:\Program Files\MATLAB\R2013a\bin and my files are saved in \F:, so what is wrong.
regards,
nadia
You are trying to save your files in C:\Program Files\MATLAB\R2013a\bin, this is not allowed by windows
savefile = fullfile('F:','pqfile.mat');
p = rand(1, 10);
q = ones(10);
save(savefile, 'p', 'q');
Dear,
I wrote in my file
savefile = fullfile('F:','Rfile.mat');
R=[1 2 3];
save(savefile, 'R');
it didn't gave me error, but when I wrote in another file
load(savefile, 'R');
it gave error, so how I call it.
regards,
nadia
What error does it give when you do the load() ?
Jan
Jan on 2 Aug 2015
Edited: Jan on 2 Aug 2015
Which error do you get? Is the variable "svefile" undefined? If so, define it again in the other function.
Dear,
when I write
savefile = fullfile('F:','Rfile.mat');
R=[1 2 3];
save(savefile, 'R');
load(savefile, 'R')
in the same file is ok, their is no error but there is no result in the command window, and there is no meaning because I want to load it from another file. when I write in another file
load(savefile, 'R');
I get this error
error in simulation (line1)
load(savefile, 'R');
all my work is stop because of these commands, I really depend on them.
thanks for any help
nadia
Can you explain the meaning of [when I write in another file] ? What is this file? a script or a function?
Dear Azzi,
its script. My code start like this
load(savefile, 'R');
clc;clear; close all;
tic
and so on and I put break point in front the second line then run the code to see if I can load the vector R.
regards
@nadia nadi: Please post the complete error message. Currently we see only, where the error occurs, but no description of the problem.
If the load command is found in the first line, the code cannot be a function, because function must have at least one line starting with "function". Therefore your "other file" must be a script. Scripts use the workspace (the list of variables) of the calling code. Does the variable "savefile" exist in the calling code?
I assume, the problem is trivial: You simply did not define the variable "savefile" in the workspace, which is active when the load command appears. The concept of local variables is explained exhaustively in the Getting Started chapters of the documentation. You cannot work with Matlab efficiently without learning the basics. Even the forum is not the right location to ask, what is explained clean and clear in the docs.

Sign in to comment.

Categories

Asked:

on 30 Jul 2015

Commented:

on 25 Jan 2018

Community Treasure Hunt

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

Start Hunting!