Reading uneven datasets from .mat file

Hello all,
I'm trying to load datasets from .mat file in MATLAB. However, my datasets are not evenly distributed due to which I cannot import my entire dataset into MATLAB. My datasets are spilt in the intervals of 20 and 21. Is there a way to import the entire datasets at once into MATLAB?Please provide some suggestions to resolve this issue.
Thanks in advance!

댓글 수: 3

Cris LaPierre
Cris LaPierre 2023년 5월 11일
Please share your mat file. You can attach it to your post using the paperclip icon.
Hello Chris,
I'm dealing with heavy file (3GB) and I can't attach it. But, I'm sharing the code that I'm using to read and write the datasets.
Data = load('testcase.mat');
N = 21:21:30215;
Tdata = repmat(Data.Frame21, [1 1 length(N)]);
for n = 1:length(N)
name = sprintf('Frame%d', N(n));
Tdata(:,:,n) = Data.(name);
end
The issue is that my datasets are not evenly distributed and after reading some sets (initially), MATLAB generates error stating frames cannot be found.
Stephen23
Stephen23 2023년 5월 11일
편집: Stephen23 2023년 5월 11일
Rather than generating the fieldnames youself (including non-existent ones), why not just loop over the existing fieldnames?:
If the aim is simply to extract to a numeric matrix, you could use STRUCT2CELL and then CAT(3,..).
Or STRUCTFUN with a function that returns a scalar cell, then again CAT(3,...).

댓글을 달려면 로그인하십시오.

답변 (1개)

Stephen23
Stephen23 2023년 5월 11일
편집: Stephen23 2023년 5월 11일

0 개 추천

Assuming that:
  • the fields are in the required order (otherwise: you can sort them using ORDERFIELDS).
  • the MAT file contains only the data that you require (otherwise: you can specify the variables to import).
S = load('testcase.mat');
C = structfun(@(m){m},S);
A = cat(3,C{:})

댓글 수: 4

Hello Stephen,
I ran your suggestions but I'm getting an error stating:
Error using cat
Dimensions of arrays being concatenated are not consistent.
I'm attaching the image of the dataset (.mat file) after loading into MATLAB. There are 3 types of datasets and I'm only interested in extracting Frame21, Frame 42, ...., Frame 30215. But I cannot do that due to uneven distribution. Kindly suggest me how to resolve this.
Stephen23
Stephen23 2023년 5월 11일
편집: Stephen23 2023년 5월 11일
"Kindly suggest me how to resolve this."
As I wrote in my answer, you can specify which variables you want to LOAD, e.g.:
S = load('testcase.mat','-regexp','^Frame\d+$');
Sidharth Raut
Sidharth Raut 2023년 5월 11일
Hello Stephen But can I load all frames at once? I only need frames (frame21 to frame 30215), will it be possible to import everything at once?
Stephen23
Stephen23 2023년 5월 11일
"But can I load all frames at once? I only need frames (frame21 to frame 30215), will it be possible to import everything at once?"
The LOAD command i gave you in my last comment will import every variable whose name is "Frame" followed by some digits. That seems to match exactly what you describe. Did you try it? Did it not work for you?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Time Series Events에 대해 자세히 알아보기

질문:

2023년 5월 11일

댓글:

2023년 5월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by