Converting .mat to .txt which also contains cell arrays.

2 views (last 30 days)
I am using the Lidar labeler app from the lidar toolbox in MATLAB. When i annotate the pcds and save them, they are saved in .mat files. We know, when there is more than 1 object in a frame, they are stored in cell arrays. I have 9 classes in my annotation and i want to store these label data frame wise and class wise in a text file. How do i do it? For your reference, the structure of the Label data is attached.
I have tried accessing the cell arrays but failed continuously. Would really appreciate your help in this regard.
Thank you in advance!
  4 Comments
Lab
Lab on 10 Oct 2023
I would like to create seperate text files for each annotation, yes.
A JSON file is also something i want to work with soon.

Sign in to comment.

Answers (1)

prabhat kumar sharma
prabhat kumar sharma on 26 Oct 2023
Edited: prabhat kumar sharma on 26 Oct 2023
Hi Lab,
I understand that you are facing issue with storing data label wise and class wise in a text file.
To store the label data frame-wise and class-wise from the Lidar Labeler app in MATLAB, you can extract the relevant information from the saved MAT files and write it to a text file. Here's a step-by-step approach to help you accomplish this:
1. Load the label data from the .mat file:
load('label_data.mat'); % Replace 'label_data.mat' with the path to your label data file
2. Access the necessary fields in the label data structure:
frameData = label_data.frameAnnotations;
3. Iterate through each frame and extract the class-wise labels:
numFrames = numel(frameData);
numClasses = numel(label_data.labelDefinitions);
% Create a cell array to store the labels for each frame and class
labels = cell(numFrames, numClasses);
for i = 1:numFrames
for j = 1:numClasses
% Extract the labels for each class in the current frame
labels{i, j} = frameData(i).objectAnnotations(j).classification;
end
end
4. Write the label data to a text file:
filename = 'label_data.txt'; % Specify the filename for the text file
fid = fopen(filename, 'w'); % Open the file for writing
for i = 1:numFrames
fprintf(fid, 'Frame %d:\n', i);
for j = 1:numClasses
fprintf(fid, 'Class %d: ', j);
% Write the labels for each class in the current frame
if isempty(labels{i, j})
fprintf(fid, 'No label\n');
else
fprintf(fid, '%s\n', labels{i, j});
end
end
fprintf(fid, '\n');
end
fclose(fid); % Close the file
You can use the above code for reference for your MAT file and class and frame label.
After executing the above code, you will have a text file (`label_data.txt`) containing the label data frame-wise and class-wise. Each frame will be labelled with the corresponding class information. If there is no label for a specific class in a frame, it will be indicated as "No label" in the text file.
I hope it helps!

Categories

Find more on Labeling, Segmentation, and Detection in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!