Error: Cell contents reference from a non-cell array object - matlab

when I run the code below, I got the following error :
Cell contents reference from a non-cell array object.
folder can take 1 or several folders
for x=1:numel(folder)
y{x} = fullfile(folder{x},'Status.xml');
getFile = fileread(char(y{x}));
content{x} = strtok(getFile ,';');
end
>>whos folder
Name Size Bytes Class Attributes
folder 1x1 941 struct
>> numel(folder)
ans=
1

Assuming folder is a cell array, I believe this should work:
y = cell(numel(folder), 1);
for x=1:numel(folder)
y{x} = fullfile(folder{x},'Status');
getFile = fileread(char(y{x}));
content{x} = strtok(getFile ,';');
end
Your error is most likely with y{ii}. I guess y is not pre-defined.
Also: you use ii as index in y, whereas you use x in the loop.
In case folder is a normal matrix, have you tried using just folder(x)?
UPDATE:
I see from your updated question that folder is a struct, not a cell. Try the following, where you substitute .field with whatever you name your entries in folder.
y = cell(numel(folder), 1);
content = cell(numel(folder), 1);
for x=1:numel(folder)
y{x} = fullfile(folder(x).field,'Status');
getFile = fileread(char(y{x}));
content{x} = strtok(getFile ,';');
end

Related

Matlab imwrite - a filename must be supplied

I have an image in the directory:
C:\Users\me\folder\A1B1\A\0001.bmp
I have multiple directories ('A1B1\A', 'A1B1\B', 'A3B1\A', ...). After reading in that image and modifying it, I store the image under the variable I. I tried to save that modified image as 0001_1.bmp using
a = 'C:\Users\me\folder'
b= 'A1B1'
c = 'A'
img = '0001.bmp'
sp=strsplit(img(1), '.');
full = fullfile(a, b, c);
scat=strcat(full, '\', sp(1), '_1.bmp');
imwrite(I,scat);
but I get
Error using imwrite>parse_inputs (line 523)
A filename must be supplied.
How can I resolve this?
Your current code produces a cell (not a character array) containing the following file name:
C:\Users\me\folder\A1B1\A\0_1.bmp
which seems to diverge from the desired output:
C:\Users\me\folder\A1B1\A\0001_1.bmp
This should fix your problem:
a = 'C:\Users\me\folder';
b = 'A1B1';
c = 'A';
img = '0001.bmp';
sp = strrep(img, '.', '_1.');
full = fullfile(a,b,c,sp);
imwrite(I,full);

How do you load a file whose file name is stored in an array?

here is my code:
function [] = plotavg (x)
files = dir('*.mat');
for c=1:length(files)
load files(c);
d=0;
if start_month == x
for i=1:length(data)
d = d + data(i);
end
end
end
I don't know how to write it so that the load function loads the file listed in that index of the array
Thanks!
dir returns a struct with some field names. One of them is "name", i.e. the name of the file. If you type e.g. files(1) in the console, you will see the fields you get for each item in your directory.
Replace
load files(c);
with
load(files(c).name);
and it should work.

Dealing with numbered variable names MATLAB

This is a bit of a long problem:
I am building an extension to some already existing software that outputs data as a structure array each time it is run. They always have the same name (structureArray)
I want to take all of these structure arrays and use them for analysis in a single code with for loops and cell arrays.
So I now have 3 structure arrays from this existing software, which I have named structureArray1, structureArray2 and structureArray3. I have used the following method for putting each of these into a cell array called "storage".
[filename, pathname] = uigetfile('*.mat','Please select your structure arrays',...
'Multiselect','on');
storage = cell(1,numel(filename));
for x = 1 : numel(filename)
storage{x} = load([pathname filename{x}]);
end
Now here's the problem:
in each structureArray(1,2,3) (now within "storage") there is a matrix called "magV". I would like to have a 1x3 cell array, with the first cell containing magV from structureArray1, the second cell containing magV from structureArray2 and so on...
My attempt so far:
magnitude_V = cell(1,numel(storage));
for y = 1 : numel(storage)
magnitude_V{y} = storage{1,y}.structureArray1.velocityMap.magV;
end
But because all of the structure arrays have a different number at the end, I can't use this method...
Thank you so much for any help because this is driving me mad -.-
You can refer to a structure's fied by a string in parenthesis, e.g. sometruct.('somefield'):
magnitude_V = cell(1,size(storage,2));
for y = 1 : size(storage,2)
magnitude_V{y} = storage{y}.(['structureArray' num2str(y)]).velocityMap.magV;
end

Reconstruct directories from file MATLAB

Thanks for your help.
The problem is:
I need the user to select a file based on an extension lets say .tif. I used the standard method, i.e.
[flnm,locn]=uigetfile({'*.tif','Image files'}, 'Select an image');
ext = '.tif';
But I need to fetch other image files from other subdirectories. Say the directory name returned to locn is: /user/blade/checklist/exp1/trial_1/run_1/exp001.tif. Image goes to exp100.tif.
I want to access:
/user/blade/checklist/exp1/trial_1/run_2/exp001.tif.
Also access:
/user/blade/checklist/exp1/trial_2/run_2/exp001.tif.
Up to trial_n
But if I list directory in /user/blade/checklist/exp1/, I get all folders therein from where I can reconstruct the right path. The naming structure is orderly.
My current solution is
[flnm,locn]=uigetfile({'*.tif','Image files'}, 'Select an image');
ext = '.tif';
parts = strsplit(locn, '/');
f = fullfile(((parts{end-5}),(parts{end-4}),(parts{end-3}),(parts{end-2}),(parts{end-1}));
Which is really ugly and I also lose the first /. Any help is appreciated.
Thanks!
First, get the file location as you did; note a small change I've made to make use of the variable ext.
ext = '.txt';
[flnm,locn]=uigetfile({['*',ext]}, 'Select an image');
parts = strsplit(locn,'/');
root = parts(1:end-4);
parts has 2 information - 1) path of the selected file; 2) path of your working folder, checklist, which you need. So root has the working folder.
Then, list out all the files you wanted, and put them in a cell array.
The file names should contain partial (subfolder) paths; it's not difficult to follow the pattern.
flist = {'trial_1/run_1/exp001.tif', ...
'trial_1/run_1/exp002.tif', ...
'trial_1/run_2/exp001.tif', ...
'trial_2/run_1/exp001.tif', ...
'trial_2/run_2/exp001.tif'};
I just enumerated a few; you can use a for loop to automatically generate trial_n and expxxx.tif. An example code to generate the complete file list (but not "full paths") -
flist = cell(10*2*100,1);
for ii = 1:10
for jj = 1:2
for kk = 1:100
flist{sub2ind([10,2,100],ii,jj,kk)} = ...
sprintf('trial_%d/run_%d/exp%03d%s', ii,...
jj, kk, ext);
end
end
end
Finally, use strjoin to concatenate the first part (your working folder) and second part (needed files in subfolders). Use cellfun to call strjoin for each cell in the file list cell array, so for every file you want you get a full path.
full_flist = cellfun(#(x) strjoin([root, x],'/'), ...
flist, 'UniformOutput', false);
Example output -
>> locn
locn =
/home/user/Downloads/exp1/trial_1/run_1/
>> for ii = 1:5
full_flist{ii}
end
ans =
/home/user/Downloads/trial_1/run_1/exp001.tif
ans =
/home/user/Downloads/trial_1/run_1/exp002.tif
ans =
/home/user/Downloads/trial_1/run_2/exp001.tif
ans =
/home/user/Downloads/trial_2/run_1/exp001.tif
ans =
/home/user/Downloads/trial_2/run_2/exp001.tif
>>
Note: You can either use
strjoin(str1, str2, '/')
or
sprintf('%s/%s', str1, str2)
They are equivalent.

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.