How to convert string to filename when MATLAB is unable to identify the file ext?

I have a set of .raw files to process, whose filenames have been generated as strings. I am unable to convert the generated string to filename because MATLAB is unable to identify .raw file. Is there any way to identify the strings as .raw extension files.
My code- Version1:
% x is input by user using prompt command, x is an integer
s1 = {'C:\Users\User\Desktop\dna\spot 1\'};
s2 = {'0'};
s4 = {'.raw'};
filename1 = fullfile('C:\Users\User\Desktop\pdna ...
\Database_of_images_for_microstructure_research ...
\spot 1\0',x,'.raw');
Error1: Error using fread Invalid file identifier. Use fopen to generate a valid file identifier.
My Code: Version2:
% x is input by user using prompt command, x is an integer
s1 = {'C:\Users\User\Desktop\dna\spot 1\'};
s2 = {'0'};
s4 = {'.raw'};
filename = strcat(s1,s2,num2str(x),s4)
if exist('filename')
[processing the .raw file]
end
Error: Unable to identify

Answers (2)

Your first attempt will create a filename that has '...spot 1\0\\.raw' as the ending string which, I'm sure isn't the file name you're trying to generate. fullfile converts the numeric value of x into a character and adds another path separator prior to the extension string. fullfile is useful really only for building the subdirectory string; then concatenate the extension to that. There should be a function for the specific syntax in Matlab, I agree, but there isn't.
What you need to do is to also convert the numeric value of x to a string. I'm not sure what the desired name is but if you have a bunch of numeric values with leading zeros, the better way is to use num2str with a format string--
>> x=1;
>> s=num2str(x,'%02d')
s =
01
>>
Also, since you have file names that include embedded spaces, you most likely need to surround the full name with double quotes (") or the OS may truncate at the space and causes problems. That is write
filename = ['"' fullfile('C:\...', 'subpath' num2str(x,'%02d')) '.raw"'];
and you should find happiness...
I'll note that if there are a series of files of this general naming convention, rather than building the file name this way, consider using dir with an appropriate wildcard expression to return the desired group and then iterate over it.
Or, use the gui tools of uigetfile and let the user simply select the file from a classic directory view.

1 Comment

You should not be using double-quotes around filenames that are passed to imread() or fopen() or the like. Double-quotes should only go around filenames that are being made part of a command given to a shell, such as if you are constructing system() or dos() arguments.

Sign in to comment.

You can use the debugger to step through your code line by line. Then you can inspect what's going on in detail.
filename1 = fullfile(['C:\Users\User\Desktop\pdna', ...
'\Database_of_images_for_microstructure_research\spot 1\0'], x, '.raw');
Now examine the file name. What you really want is:
filename1 = fullfile(['C:\Users\User\Desktop\pdna', ...
'\Database_of_images_for_microstructure_research\spot 1\0'], [x, '.raw']);
Or does the "0" belong to the file name also?
Whenever you open a file, check the success and post an error message:
fid = fopen(filename, 'r');
if fid == -1
error('Cannot open file: %s', filename);
end
A file name must be a string, not a cell string:
s1 = {'C:\Users\User\Desktop\dna\spot 1\'};
s2 = {'0'};
s4 = {'.raw'};
filename = strcat(s1{1}, s2{1}, num2str(x), s4{1})
For the dangerous exist command it is required to specify the type: if exist('filename') checks, if a variable with the name "filename" is existing. You want:
if exist(filename, 'file')
But notice, that this is true for folders also.

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 26 Jun 2015

Edited:

Jan
on 4 Jul 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!