Datastore command for matrix
3 views (last 30 days)
Show older comments
Hi! I created a squared matrix R (2624x2624) with mapreduce. The output is
R.Key = 1
R.Value = 2624x2624 double;
I'd like to create a datastore from R.Value, but using the commands
>> save('R')
>> ds = datastore('R.mat','ReadSize',50)
Error using datastore (line 94)
Cannot determine the datastore type for the specified location.
Specify the 'Type' name-value pair argument to indicate the type of datastore to create.
gives me the error above. Please help me. Thanks!
0 Comments
Answers (1)
Aditya
on 23 Jan 2025
Hi Luca,
To handle your matrix and create a datastore, you have a couple of options. You can either convert your matrix to a table and save it as a '.csv' file or use the 'fileDatastore' function to directly work with '.mat' files. Here's how you can achieve this:
1] Using CSV and datastore:
MATLAB's 'datastore' is typically used for tabular data or text files, and not directly for matrices in '.mat' files. Thus, one approach is to convert your matrix to a table and save it as a '.csv' file, which is compatible with 'datastore'.
Here's a sample code for this:
% Convert the matrix to a table
matrixTable = array2table(R.Value);
% Save the table as a CSV file
writetable(matrixTable, 'matrixData.csv');
% Create a datastore from the CSV file
ds = datastore('matrixData.csv', 'ReadSize', 50);
2] Using fileDatastore:
If you prefer to work with the '.mat' file directly, you can use the 'fileDatastore' function. This is particularly useful for large datasets that you want to access in chunks.
Here's how you can use it:
% Define a custom read function for the .mat file
readFcn = @(filename) load(filename, 'R');
% Create a fileDatastore for the .mat file
fds = fileDatastore('R.mat', 'ReadFcn', readFcn, 'FileExtensions', '.mat');
% Read the data
data = read(fds);
% Access the matrix
matrixData = data.R.Value;
To read more about this functions please refer to the below MATLAB documentations:
0 Comments
See Also
Categories
Find more on Datastore in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!