0

New to matlab and I need some help.

I need to create a .mat file , using matObj or save(), that has some information that will be passed from some variable. Lets say that variable x = 1,2,3,4,5

1|2|3|4|5|

Then I need to save that in test.mat

Then I need to load that file and save something like,

6|7|8|9|10|

So I get

1|2|3|4|5|

6|7|8|9|10|

and so on.

So every time I save it goes to a new row. The numbers that go inside they are not random the above numbers are just there to make things simple to see.

Can someone help me out.

1
  • 1
    What you are trying to do is very basic and it is well covered in Matlab's generally good documentation. I suggest that you review the on-line documentation for the functions save and load, experiment a bit on your own, and return here when you get stuck. One of Matlab's strengths is its suitability for prototyping, which is just a fancy word for messing around until you get it right. You are very unlikely to do any damage to anything messing around, so dive in. Commented Aug 30, 2012 at 16:40

2 Answers 2

1

You are describing two different problems here. The first is saving and loading of data.

Saving is easy:

x = 1:5;
filename = 'myFile.mat'
save(filename, 'x'); %notice that I used the string name of the variable

Likewise loading is also simple:

filename = 'myFile.mat';
data = load(filename); % loaded variables are placed in a struct to prevent overwriting workspace variables
x = data.x;

The 2nd problem can be solved using concatenation:

lets say you want to convert the vector 1 2 3 into the matrix:

1 2 3
1 2 3

You can simply call:

v = 1:3;
m = cat(1, v, v);

Likewise you can add an additional row to the existing matrix using the same command:

m = cat(1, m, v);
Sign up to request clarification or add additional context in comments.

Comments

0

I'm sure any amount of googling will get you how to save a variable to a mat file - The matlab docs are absolutely spectacular, and such a simple operation will be covered along with examples showing exactly how to use the functions.

As for the second part, use the concatenation property

new = [old1 old2];

to concatenate horizontally, and

new = [old1;old2];

to concatenate vertically. Then resave the same way that you just learned via google.

Hope this helps, and in the future, i guarantee 99% of the answers to a new user's questions will be in the top two google search results if you append "matlab" to your search. The Mathworks really set the bar on documentation in my opinion. (Of course, I last used MATLAB 3 years ago)

1 Comment

This part was the most helpfull. Best regards

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.