I am trying to save some data from current workspace in Matlab to a different folder. I tried using
save('c:\stp\vtp\train.txt','data','-ASCII');
where data is a double matrix. It gives me error message
??? Error using ==> save
Unable to write file c:\stp\vtp\train.txt: No such file
or directory.
I tried using fullfile syntax, even then it is the same case. My current working folder is in different path.
You probably need to run mkdir first. For example:
%Some data to save
x = 1;
%Try to save it in a deep, non-existent directory
save(fullfile(tempdir,'sub1','sub2','sub3','sub4','data.mat'),'x');
% This will probably recreate your error
%To fix, first create the directory
mkdir(fullfile(tempdir,'sub1','sub2','sub3','sub4'))
%Now save works
save(fullfile(tempdir,'sub1','sub2','sub3','sub4','data.mat'),'x') %No error
Related
I got a batch of processed datasets in eeglab. I want to export all of them into txt in a batch, however, it seems like must be done file by file.
I am new to eeglab and matlab. Could someone please help me with this?
This code was tested by being run in my directory with EEGLAB in it. In my case there are 2 data sets with associated .fdt files in that directory as well. If your .set files are in a different directory from EEGLAB, you will have to change the code to find them there. The script must be in the EEGLAB directory, or else the EEGLAB source must be in your PATH, but I think that putting EEGLAB's code in your PATH is not a recommended setup.
I use regular expression (regexp) to find which files are .set files and to build the output filenames. If you are not familiar with regular expressions, just do a websearch.
% read all the files in the directory
files = dir();
% parse directory contents for .set files
sets = {};
idx = 1;
for n=1:length(files)
if(regexp(files(n).name,'.set'))
sets{idx} = files(n).name;
idx = idx+1;
end
end
% load the data sets and write the data to appropriate filename
for n=1:length(sets)
% change the argument after filepath to the path your EEGLAB
% instalation is in
% note the double '\' directory delimiter is for Windows
EEG = pop_loadset('filename', sets{n},'filepath','C:\\Users\\david.medine\\matlab\\toolboxes\\eeglab2019_0\\');
EEG = eeg_checkset( EEG );
outputfilename = sprintf('%stxt', sets{n}(1:regexp(sets{n}, '.set')))
writematrix(EEG.data, outputfilename);
end
By the way, I knew what functions to call from EEGLAB to load the .set files by checking >> EEG.history. That will show all the Matlab code that went on behind the GUI scenes in your EEGLAB session.
EEGLAB stores the data in vectorized orientation. If you want multiplexed simply transpose the matrix:
writematrix(EEG.data', outputfilename);
I generated and saved large number of data files using Octave, and now I need to open them in MATLAB as part of analyzing them. MATLAB spits out this error.
Error using load
Unable to read MAT-file
[file path]/PhPar_40.mat: not a binary MAT-file.
Try LOAD -ASCII to read as text.
Trying its suggestion of load -ASCII then gives this error.
Error using load
Number of columns on line 2 of ASCII file
[filepath]/PhPar_40.mat must be the same as previous lines.
I (now) understand that Octave is capable of saving in a MATLAB readable format, but re-creating these data files would take an inordinate amount of time and really isn't an option. Is there a way to get MATLAB to read these files?
MATLAB can't open these files because these are not saved by octave properly. Try saving them in octave by following command:
save -mat7-binary '[filepath]/PhPar_40.mat' 'm'
If you have large number of files, you can place all files in folder and then run an iterator to read all load and save in correct format automatically. This iterator will look like:
file = dir('[filepath_read]/*.mat');
index = 1;
while (index==length(file)+1)
m = load('file(index).name')
save -mat7-binary strcat("[filepath_write]/", file(index).name, ".mat") 'm';
index = index+1;
pause(1);
endwhile
Once you have all the files converted to right format, load them in MATLAB. I hope it will solve your problem
I am trying to load files from my directory with matlab. The code is fairly simple :
for j =1:8
people_names=dir('~/Desktop/Directory/Data/*.mat');
people_name=people_names(j).name
resp=load('~/Desktop/Directory/Data/people_name');
However, the load command fail because it reads "people_name" as a string and not its value.
D'oh. Your first statement in your for loop should be outside of it. You want to find all files first, then loop over each file. You're doing that inside your loop statement, and that will probably not give you what you want.
You also are using load wrong. You'd want to use the actual string of people_name itself. You also will want to loop over all possible file names, not just the first 8:
people_names=dir('~/Desktop/Directory/Data/*.mat'); %// Change
for jj = 1:numel(people_names) %// Change
people_name=people_names(jj).name;
resp=load(people_name); %// Change
%// Rest of your code here....
%//...
end
Working on MATLAB 2008, I am trying to save all the images my scripts produce when invoking the "plot" function.
In order to achieve this, I have two possible solutions:
Either I write another function having the same parameters and perform a search/replace in the *.m sources
or I override the plot.m file so that I write the image into a specific directory when generated.
I did many searches and I am unable to find the plot.m source file. The only file I found is located in the toolbox directory and does not contain any code (except some commented documentation).
you can simply use the print command and save them into a directory that you can also make using the mkdir command.
Sample code
clc; close all; clear all;
x = 1:10;
y = x.^2;
plot(x,y)
if exist('plots','dir') ~= 7
mkdir('plots'); % make directory if it does not exist
end
print -dpdf ./plots/jawn.pdf
Read the print documentation, to learn how to print in other file formats
Also, I would not suggest overriding the plot command, and you will likely not be able to find the source code for plot.m because that is proprietary MATLAB code
I have 110 files named time1.mat, time2.mat ..., time110.mat. I want to load these matrices into the MATLAB workspace.
I have always used load -'ASCII' matrix.mat to load an ASCII matrix file in the current folder.
So I tried doing
for i=1:10
filename=strcat('time',int2str(i),'.mat');
load -'ASCII' filename
end
But I am getting a MATLAB error as
??? Error using ==> load
Unable to read file filename: No such file or directory.
�
Of course the string filename seems to be evaluated correctly by MATLAB as time1.mat. in the first iteration where it crashes at the load line.
Any suggestions how I should do this?
Use load(filename, '-ascii')