Using 'OR' or 'ELSEIF' with a string

Hi, I have a basic question. I am reading a file named as Basefilename="Filenameversion1.production_time.improvement_date.time_frame.hdf".
The code is not reading files from a folder containing different versions i.e "Filenameversion2......hdf", "Filenameversion3......hdf".
Code only reads filename mentioned in Basefilename.The remaining filename is same only version name is changed.
How can i do modification so that matlab reads file even if 'version' is changed.
Thanks alot

Answers (2)

I am not certain that I understand what you want to do, but this may be one approach:
ver_nr = 2;
Basefilename=sprintf('Filenameversion%d.production_time.improvement_date.time_frame.hdf', ver_nr)
produces:
Basefilename = Filenameversion2.production_time.improvement_date.time_frame.hdf
Is that what you want to do?

10 Comments

Yes,something similar. By this method i have to update ver_nr everytime,is there any way i dont need to change it..?
I have modified the basefilename as:
Basefilename =Filenameversion%d.production_time.improvement_date.%s.hdf
Using %d it does not scan for time frame(%s is scaning time) i,e processing is as: Filenameversion1.production_time.improvement_date
First, you can define ‘ver_nr’ as a loop index variable. If the numbers are discontinuous, you can define them as a specific vector, for example: for ver_nr = [1 2 5 8 10] as the start of the loop.
Second I do not understand what the ‘%s’ refers to or what you want to do with it. That was not part of your original post.
JB
JB on 3 Jan 2016
Edited: JB on 3 Jan 2016
%s is scaning time_frame ,as time_frame is different for every file Basefilename="Filenameversion1.production_time.improvement_date.time_frame.hdf"
JB
JB on 3 Jan 2016
Edited: JB on 3 Jan 2016
Is there a way to use 'or' or 'elseif' for e.g:
if Basefilename="Filenameversion1.production_time.improvement_date.time_frame.hdf"
else if
Basefilename="Filenameversion2.production_time.improvement_date.time_frame.hdf"
How do the scanning time_frame and version number relate?
I am now lost. I have no idea what you are doing or what you want to do.
No, you can either use numbers in the "if" test (like if num1==num2), or you can compare two strings with string functions like strcmp(str1, str2), strcmpi(), or strfind().
In simple words i want the following,
if Basefilename="Filenameversion1.production_time.improvement_date.time_frame.hdf"
else if
Basefilename="Filenameversion2.production_time.improvement_date.time_frame.hdf"
Sorry for inconvience.. my bad..
I still don’t understand, but if you want to do comparisons with string variables, use strcmpi, strncmpi, or related functions (links to those in and at the end of those pages).
A comparison would have == not =. So I don't know if you're wanting to do
if Basefilename == "Filenameversion1.production_time.improvement_date.time_frame.hdf"
% Some code to do if this is true.
elseif Basefilename == "Filenameversion2.production_time.improvement_date.time_frame.hdf"
% Some code to do if this is true.
end
Or if you're trying to do
if num1 == num2
% Assign Basefilename
Basefilename = "Filenameversion1.production_time.improvement_date.time_frame.hdf"
else
% Assign Basefilename
Basefilename = "Filenameversion2.production_time.improvement_date.time_frame.hdf"
end
Which is it? Or is it neither?

Sign in to comment.

Asked:

JB
on 3 Jan 2016

Edited:

on 3 Jan 2016

Community Treasure Hunt

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

Start Hunting!