How to split .mat data into different variables?

I have .mat file in workspace which is 5120x1 dimention. I want to split that into (1024x1), (2048x1), (3072x1), (4096x1) and (5120x1) in 5 different varibales.
Is it possible with mat2cell function?

1 Comment

You haven’t told us what classes they are and haven’t shared the file either.

Sign in to comment.

 Accepted Answer

You can't have a file in your workspace, only variables (and objects if you don't count those as variables).
There are some guesses involved in what you want. Below is my best guess.
data=rand(5120,1);%generate some random data
c=num2cell(1024:1024:5120);
for n=1:numel(c)
c{n}=data(1:c{n});
end
c
c = 1×5 cell array
{1024×1 double} {2048×1 double} {3072×1 double} {4096×1 double} {5120×1 double}
You shouldn't want to put it in seperately named variables, as that will prevent you from using indexing to get to these results. However, if you insist, you can use the deal function to split the cell.
[foo,bar,foobar,f00,barr]=deal(c{:});
foo
foo = 1024×1
0.5291 0.7418 0.5861 0.9820 0.9140 0.5865 0.2450 0.8748 0.7799 0.4680

8 Comments

if the data is 51200, and i need 1024 chunk of that 51200. So how can i save that 50 chunks of 1024 into 50 variables .?
You can, but you shouldn't. Why do you think you need 50 different variables? Why wouldn't the cell array do the job? I haven't seen a situation yet where this would be required. Maybe yours is the first, but without an explanation of what you want, I'm going to assume you shouldn't be using eval to create variable names.
Acctully i have dataset of (2x1024x 31200) in which 2 show real and imaginary part and that each signal is 1024 bit long and there is 31200 examples. But i need only fist 50 examples to test the model accuracy.
that why i need 50 variable that i store in my drive and then i give that path to the model.
You don't need 50 variables.
Either you provide the input in a loop to a function, in which case this cell array will work just fine, or your function loads data from a mat file, in which case it will expect a specific variable name, which also doesn't require 50 different variables.
If you want help implementing the input to your model, feel free to comment or ask a new question. As for this specific question; did my answer solve your question? If so, please consider marking it as accepted answer.
because i want to save that data thats why i need 50 variable. Then i can save that data. Without assigning value from (0:1024) and (1025:2048).... etc how can i save it. For 5 variable its easy but i cant write 50 variable like in
[foo,bar,foobar,f00,barr]=deal(c{:}); function.
Well , that’s why naming variables is discouraged. Why would you do that intentionally??
for n=1:numel(c)
data=c{n};
filename=sprintf('data_part_%02d.mat',n);
save(filename,'data')
end
This will write the 50 mat files you appear to want.
You still haven't explained how the input to your later function works. We might be able to suggest improvements.
The earlier code you give me is giviing output of (1024,1) 5 times but i need (0:1024),(1025:2048)... like that.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!