23 views (last 30 days)
Show older comments
imaging on 16 Jul 2024 at 12:59
Answered: Image Analyst about 2 hours ago
Open in MATLAB Online
hello i would like to ask how to save inputData(:,:,idxSelected(i)) as an image file. thanks very much.
function [data,labelsVec,timestamps] = loadCSIDataset(fileName,label,visualizeData)
% loadCSIDataset Loads and visualizes the pre-recorded CSI dataset
% [DATA,LABELSVEC,TIMESTAMPS] =
% loadCSIDataset(FILENAME,LABEL,VISUALIZEDATA) loads the dataset that
% contains the data with the label (LABEL). Pre-recorded CSIs are
% visualized if (VISUALIZEDATA) is true. The function returns the
% pre-recorded beacon frame CSI (DATA), related timestamps (TIMESTAMPS),
% and the categorical labels vector (LABELSVEC).
% Copyright 2022-2024 The MathWorks, Inc.
arguments
fileName {char,string}
label (1,1) string
visualizeData = true;
end
% Load the pre-recorded dataset
datasetDir = which(fileName);
loadedData = load(datasetDir);
data = loadedData.data;
labelsVec = categorical(repmat(label,size(data,ndims(data)),1));
timestamps = loadedData.timestamps;
disp(['Dimensions of the ' char(label) ' dataset (numSubcarriers x numPackets x numCaptures): ' '[' num2str(size(data)) ']'])
% Visualize the dataset
if visualizeData
plotSamplesFromDataset(data,label);
end
% Plot samples from the pre-recorded dataset
function plotSamplesFromDataset(data,mode)
% Plot at most three random samples of the dataset
inputData = abs(data); % Visualize only the magnitude of the CSI
numTotalCaptures = size(inputData,ndims(inputData));
numPlots = min(3,numTotalCaptures);
idxSelected = sort(randperm(numTotalCaptures,numPlots));
figure;
T = tiledlayout(2,numPlots,'TileSpacing','compact');
% Plot 1 - CSI image
for i = 1:numPlots
nexttile
imagesc(inputData(:,:,idxSelected(i)));
colorbar;
xlabel('Packets');
ylabel('Subcarriers');
title(['Raw CSI (#' num2str(idxSelected(i)),')']);
end
% Plot 2 - Normalized CSI periodogram
for j = 1:numPlots
nexttile
imagesc(csi2periodogram(inputData(:,:,idxSelected(j))));
colorbar;
clim([0 1]);
xlabel('Temporal Index');
ylabel('Spatial Index');
title(['CSI Periodogram (#' num2str(idxSelected(j)),')']);
title(T,['Randomly Selected Samples of "', char(mode) '" Data']);
set(gcf,'Position',[0 0 650 450]);
end
end
end
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Answers (2)
Muskan about 23 hours ago
Hi,
As per my understanding, to save the "inputData(:,:,idxSelected(i)") as an image file, you can use MATLAB's "imwrite" function. This function writes an image to a file in various formats such as PNG, JPEG, TIFF, etc.
You can follow the following steps:
- Convert the Data to a Suitable Format: Ensure the data is in a format that "imwrite" can handle. Typically, this means converting the data to "uint8" or "double" and normalizing it if necessary.
- Save the Image: Use "imwrite" to save the image.
You can also refer to the following documentation of "imwrite" for a better understanding: https://www.mathworks.com/help/matlab/ref/imwrite.html
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
imaging about 23 hours ago
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2137673-saving-elements-of-a-dataset-as-images#comment_3212792
thanks a lot for your help.
Sign in to comment.
Image Analyst 2 minutes ago
Open in MATLAB Online
You want to save inputData(:,:,idxSelected(i)), which is a variable internal to a Mathworks written function called loadCSIDataset. Not exactly sure what that function is but we recommend never changing a built-in function from a Toolbox written by the Mathworks.
What you should do is to make a copy of that function in the current folder, or a folder of your own earlier in the search path, with a different name, like myLoadCSIDataset.m. Then you will modify your copy, not the original function.
Then in the for loop you can construct a filename with sprintf and fullfile and then save the image slice with imwrite. Here is a snippet with the relevant part that you should modify.
% Define the name of the output folder where you want to save these images.
outputFolder = 'C:\whatever'; % TODO: CHANGE THIS LINE.
if ~isfolder(outputFolder)
mkdir(outputFolder);
end
% Plot 1 - CSI image
for k = 1 : numPlots % Use k instead of i (the imaginary constant) as an iteration variable.
sliceNumber = idxSelected(k);
nexttile
imagesc(inputData(:, :, sliceNumber));
colorbar;
xlabel('Packets');
ylabel('Subcarriers');
caption = sprintf('Raw CSI (Slice #%d))', sliceNumber);
title(caption, 'FontSize', 18);
drawnow; % Force it to update the screen display immediately.
% Now make output file name for a lossless compression PNG format file.
baseFileName = sprintf('Slice %3.3d.png', sliceNumber);
fullFileName = fullfile(outputFolder, baseFileName); % Prepend output folder.
% Now save this image slice to disk.
imwrite(inputData(:, :, sliceNumber), fullFileName) % Save to disk.
fprintf('On iteration %d, saved slice #%d as %s.\n', k, sliceNumber, fullFileName); % Print the progress to the command window.
end
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office