Matlab sscanf error - matlab

The following code is used to read and label data from an eeg file.
Matlab gives the following error:
Error using sscanf
First argument must be a string.
Is there a way to fix this?
%% File Names reading and label generation
dataFolder= 'SMNI_CMI_TRAIN/';
fileNames = dir([dataFolder 'c*.*']);
lbl = sscanf(cat(1,fileNames.name)','co2%c%d.rd.%d');
status = lbl(1:3:end);
id = lbl(2:3:end);
ids = unique(id);
trial = lbl(3:3:end);

Related

Error by using eval in function in MATLAB

I have the following MATLAB function:
function getDBLfileL1(pathInput,Name_file,folderName)
DBL_files=dir([pathInput,'/*.DBL']); %get DBL files
fprintf('Reading DBL files ... ')
for i = 1:length(DBL_files) %loop through all DBL files
[HDR, CS]=Cryo_L1b_read([pathInput,'/',DBL_files(i).name]); %read data with ESA's Cryo_L1_read function
Coord{i}.LAT_20Hz=CS.GEO.LAT; %store values to struct
Coord{i}.LON_20Hz=CS.GEO.LON;
Coord{i}.BoundingBox_StartLATLON_StopLATLON=[HDR.START_LAT*10^-6,HDR.START_LONG*10^-6,HDR.STOP_LAT*10^-6,HDR.STOP_LONG*10^-6];
Coord{i}.FileName=[pathInput,'/',DBL_files(i).name];
end
eval([Name_file '= Coord;']);
save(['output/',folderName,'/',Name_file,'.mat'],Name_file,'-v7.3')
fprintf('done\n')
end
And it is called in the following:
getDBLfileL1(pathInput,[folderNames{i},'_',folderNames1{j}],folderNames{i}); %read Data from DBL file
The Value of Name_file is '2011_01', and I get the following error:
eval([Name_file])
Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.
Does anyone know why this error occur or how I can change the file, that I can replace the eval() function?
Thanks a lot in advance!
If I got it right, you are trying to evaluate '2011_01= Coord;' , which means that you are assigning Coord into a variable called 2011_01, and variable names cannot start with numbers

Read specific portions of an excel file based on string values in MATLAB

I have an excel file and I need to read it based on string values in the 4th column. I have written the following but it does not work properly:
[num,txt,raw] = xlsread('Coordinates','Centerville');
zn={};
ctr=0;
for i = 3:size(raw,1)
tf = strcmp(char(raw{i,4}),char(raw{i-1,4}));
if tf == 0
ctr = ctr+1;
end
zn{ctr}=raw{i,4};
end
data=zeros(1,10); % 10 corresponds to the number of columns I want to read (herein, columns 'J' to 'S')
ctr=0;
for j = 1:length(zn)
for i=3:size(raw,1)
tf=strcmp(char(raw{i,4}),char(zn{j}));
if tf==1
ctr=ctr+1;
data(ctr,:,j)=num(i-2,10:19);
end
end
end
It gives me a "15129x10x22 double" thing and when I try to open it I get the message "Cannot display summaries of variables with more than 524288 elements". It might be obvious but what I am trying to get as the output is 'N = length(zn)' number of matrices which represent the data for different strings in the 4th column (so I probably need a struct; I just don't know how to make it work). Any ideas on how I could fix this? Thanks!
Did not test it, but this should help you get going:
EDIT: corrected wrong indexing into raw vector. Also, depending on the format you might want to restrict also the rows of the raw matrix. From your question, I assume something like selector = raw(3:end,4); and data = raw(3:end,10:19); should be correct.
[~,~,raw] = xlsread('Coordinates','Centerville');
selector = raw(:,4);
data = raw(:,10:19);
[selector,~,grpidx] = unique(selector);
nGrp = numel(selector);
out = cell(nGrp,1);
for i=1:nGrp
idx = grpidx==i;
out{i} = cell2mat(data(idx,:));
end
out is the output variable. The key here is the variable grpidx that is an output of the unique function and allows you to trace back the unique values to their position in the original vector. Note that unique as I used it may change the order of the string values. If that is an issue for you, use the setOrderparameter of the unique function and set it to 'stable'

How to extract columns of data from .txt files MATLAB

I have some data in a .txt file. that are separated by commas.
for example:
1.4,2,3,4,5
2,3,4.2,5,6
24,5,2,33.4,62
what if you want the average of columns, like first column (1.4,2 and 24)? or second column(2,3 and 5)?
I think putting the column in an array and using the built in mean function would work, but so far, I am only able to extract rows, not columns
instead of making another thread, I thought i'd edit this one. I am working on getting the average of each column of the well known iris data set.
I cut a small portion of the data:
5.1,3.5,1.4,0.2,Iris-setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
delimiterln= ',';
data = importdata('iris.txt', delimiterln);
meanCol1 = mean(data(:,1))
meanCol2 = mean(data(:,2))
meanCol3 = mean(data(:,3))
meanCol4 = mean(data(:,4))
Undefined function 'sum' for input arguments of type 'cell'.
Error in mean (line 115)
y = sum(x, dim, flag)/size(x,dim);
Error in irisData(line 6)
meanCol1 = mean(data(:,1))
it looks like there is an error with handling data type...any thoughts on this? I tried getting rid of the last column, which are strings. and it seems to work without error. So i am thinking that it's because of the strings.
Use comma separated file reading function:
M = csvread(filename);
Now you have the matrix M:
col1Mean=mean(M(:,1));

MATLAB Error using importdata

I get an error at the importdata line saying
"Error using importdata (line 137)
Unable to open file."
when I write in 'specimenAl.dat' manually into the function, the program runs fine. How can I make use of the importarray function while defining the argument as an element of an array?
material = {('specimenAl.dat'), ('specimenSt.dat')};
A = importdata(material(1));
Data = A.data;
Force = Data (:,2);
Displacement = Data (:,1);
Strain = Data (:,3);
I had the same problem. fixed it by using {} in
A = importdata(material{1});
Hope this helps!

How to read a lot of DICOM files with Matlab?

I am using a script which generate a collection of strings in a loop:
'folder1/im1'
'folder1/im2'
...
'folder1/im3'
I assign the string to a variable, when I try to execute the img = dicomread(file); function I get the following error:
Error using dicomread>newDicomread (line 164)
The first input argument must be a filename or DICOM info struct.
Error in dicomread (line 80)
[X, map, alpha, overlays] = newDicomread(msgname, frames);
Error in time (line 14)
img = dicomread(file);
However, using the command line I don't get errors: img = dicomread('folder1/im1').
The code is the next:
for i=1:6 %six cases
nameDir = strcat('folder', int2str(i));
dirData = dir(nameDir);
dirIndex = [dirData.isdir];
fileList = {dirData(~dirIndex).name}; % list of files for each directory
n = size(fileList);
cd(nameDir);
for x = 1:n(2)
img = dicomread(strcat(pwd(), '/', fileList(x)));
end
cd('..');
end
What could be the error?
You've figured it out by now, haven't you.
Based on what you've written, you test
img = dicomread('folder1/im1');
when what you are having trouble with is
img = dicomread(file);
You need to actually test the line you are having trouble with. I would recommend:
putting a break point in test.m a the line img = dicomread(file). When you get to that line you can see what file is equal to. Also do whos file to make sure it is of class char and not a cell array or something random.
If you still want help, edit your original post and show the code where you assign those strings to file and tell us what happens when you type img = dicomread(file) at the command prompt.