How to read a lot of DICOM files with Matlab? - 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.

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);

Cannot create output file with saveas

i wrote that code
clear all;
clc;
addpath('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\');
h1 = dir('C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\');
for i=3:numel(h1)
%disp(h1(i,1).name);
%disp(k);
three(h1(i,1).name);
end
and the three function is
function three(filename)
%disp(filename);
q = char(39);
filename = strcat(q,filename,q)
%disp(filename);
load(filename);
And i get that error:
Error using load
Unable to read file '03a01WaM.mat': No such file or directory.
Error in three (line 7)
load(filename);
Error in run_three (line 13)
three(h1(i,1).name);
i also wrote exist('03a01WaM.mat') and the function return 2
Does anyone has an idea, what am i doing wrong?
There are multiple issues with your code.
addpath is simply unnessecary.
You are using relative path, but not cd. You have to use the full path to access the files.
You are adding a apostrophe to the filename.
Correct code would be:
directory='C:\Users\John\Documents\MATLAB\code for yannis\anger(W)\'; %'
h1 = dir(directory);
for i=3:numel(h1)
filename=fullfile(directory,h1(i,1).name);
load(filename);
end

Converting all images in a certain folder from .tiff to .jpg using MATLAB

I am trying to convert all .tiff-files inside a certain folder to .jpg.
I have tried executing
ReadImgs('home/luisa/misc','*.tiff');
using the following function:
function X = ReadImgs(Folder,ImgType)
Imgs = dir([Folder '/' ImgType]);
NumImgs = size(Imgs,1);
image = double(imread([Folder '/' Imgs(1).name]));
for i=1:NumImgs,
[pathstr,name,ext] = fileparts(Imgs(i).name);
concatena=strcat(name,'.jpg');
imwrite(y,concatena);
end
end
But I get this error:
>> codigoPruebas
Index exceeds matrix dimensions.
Error in ReadImgs (line 4)
image = double(imread([Folder '/' Imgs(1).name]));
Error in codigoPruebas (line 7)
ReadImgs('home/luisa/misc','*.tiff');
How can I resolve this?
Check the output of dir, it does return an empty struct. That's because you passed an invalid path. It's /home/luisa/misc not home/luisa/misc. Absolute path start with / relative path not.
Some additional advices in writing robust code:
Instead of [Folder '/' ImgType] use fullfile(Folder,ImgType). It's more robust (avoids duplicate file separators) and os independent.
Use im2double instead of double to convert images. This automatically scales to 0...1
There are multiple things wrong with your solution:
The error you are getting is because you are trying to access Imgs(1), even though it is empty. This is because you supplied a wrong file path: home/luisa/misc instead of /home/luisa/misc
You only read the first image, as image = double(imread([Folder '/' Imgs(1).name])); is not inside the for-loop. (And only accesses Imgs(1) instead of Imgs(i))
imwrite(y,concatena); should use image instead of y, as y is never defined.
Implementing these changes will result in:
function convertAllToJpg(Folder,ImgType)
Imgs = dir(fullfile(Folder,ImgType));
for i=1:numel(Imgs)
oldFilename = fullfile(Folder, Imgs(i).name);
[~,name,~] = fileparts(Imgs(i).name);
newFilename = fullfile(Folder, strcat(name, '.jpg'));
imwrite(imread(oldFilename), newFilename);
end
end
This is my answer, you can use it in your Matlab as a function
suppose oldFolder is store your origin image type
newFolder is store your change image type
ImgType is your origin image type
and you can change jpg whatever you want.
function convertAllToJpg(oldFolder,newFolder,ImgType)
Imgs = dir(fullfile(oldFolder,ImgType));
for i=1:numel(Imgs)
oldFilename = fullfile(oldFolder, Imgs(i).name);
[~,name,~] = fileparts(Imgs(i).name);
newFilename = fullfile(newFolder, strcat(name, '.jpg'));
imwrite(imread(oldFilename), newFilename);
end
end

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!

Why i get this following error when using dir in Matlab?

Matlab keep give me following error message :
??? Error using ==> dir
Argument must contain a string.
Error in ==> Awal at 15
x = dir(subDirs)
Below is my codes :
%MY PROGRAM
clear all;
clc;
close all;
%-----Create Database-----
TrainDB = uigetdir('','Select Database Directory');
TrainFiles = dir(TrainDB);
dirIndex = [TrainFiles.isdir];
[s subDirNumber] = size(dirIndex);
for i = 3:subDirNumber
subDirs = {TrainFiles(i).name};
subDirs = strcat(TrainDB,'\',subDirs);
x = dir(subDirs) %<-------Error Here
end
Is something wrong with the codes? Your help will be appreciated.
I'm sorry for my bad English.
The problem is with this line:
subDirs = {TrainFiles(i).name};
When you strcat on the next line, you are strcat-ing two strings with a cell containing a string. The result in subDirs is a cell containing a string which dir() apparently doesn't like. You can either use
subDirs = TrainFiles(i).name;
or
x = dir(subDirs(1))
I would recommend the first option.
When I run your code I get the error message:
??? Error using ==> dir
Function is not defined for 'cell' inputs.
What MATLAB is telling you is that when you call dir(subDirs) subDirs is a cell rather than a string which is what dir wants. Something like dir(subDirs{1,1}) will do what (I think) you want. I'll leave it to you to rewrite your code.
with subDirs = {TrainFiles(i).name}; you create a cell-array of stings. dir is not defined for that type. Just omit the {} around the name
BTW: Your code does not only list directories, but all files. Check find on the isdir attribute to get only directory's indices!