Append apostrophe as a text in a function for a searching tool

I want to write a function to search through a pool of data of thousands of mat files that are named in a speceific way (based on the input variables) now i want to search through those mat files that are in a folder, the search will be based on the input parameters which is the name of the file.
sample of file name is : K1 d1 D10 V10 S15 t1 C0.5 T0.8.mat
the code i wrote for this is
function [Load] =srch(Adress,Max_K,dist_step,Max_Len,Max_V,Sim_time,time_step,C0,TAU)
Name=append('K',mat2str(pMax),' ','d',mat2str(dx),' ','D',mat2str(xMax),' ','V',mat2str(vMax),' ','S',mat2str(tMax),' ','t',mat2str(dt),' ','C',mat2str(c0),' ','T',mat2str(tau),'.mat')
a="'"
b="'"
Load = load(append(a,Adress,'\',Name,b))
end
the native load function of matlab requires aphostrophe at the start and end of the filename and adress and i am not bieng able to incorporate the aphostrophe in my code.

3 Comments

"the native load function of matlab requires aphostrophe at the start and end of the filename and adress"
No it does not. Never has and never will.
Single quotes are absolutely not required within filenames.
You are confusing how character vectors are defined and displayed, which has nothing to do with the existence (or not) or single quote characters within the text itself. In case you want to do some reading, here are some duplicate threads where the OP also incorrectly thought that their character vectors had single quotes at the ends:
Your function is interesting:
Why are the input arguments unused? Where are the variables referred to within the function defined?
Your code would be improved by using SPRINTF and FULLFILE, e.g.:
function out = srch(Adress,Max_K,dist_step,Max_Len,Max_V,Sim_time,time_step,C0,TAU)
fnm = sprintf('K%u d%u D%u V%u S%u t%u C%f T%f.mat',pMax,dx,xMax,vMax,tMax,dt,0,tau);
out = load(fullfile(Address,fnm));
end
Good reference! I was hoping to solve the issue by providing a simple example, not to have to mention "aphostrophe".
@Fangjun Jiang Thanks alot for your prompt reply and @Stephen @Steven Lord for your valuable suggestions, i am new to matlab and will definatly give those references a read.
@Stephen the input arguments in this function are the input arguments of another function so to avoid mixing them up i used different variable ids, however in this function they doesnot match i have changed them later that was a mistake.

Sign in to comment.

 Accepted Answer

Find a .mat file in your current folder and try this:
name='MyData.mat';
folder=pwd;
filename=fullfile(folder,name);
data=load(filename);

2 Comments

This works, and i am able to locate my required file, but it doesnot load properly. still need to add the aphostrophe.
when i manually load the file thr following appears and the exact file is shown in the workspace
load('K3 d2 D12 V20 S26 t2 C0.7 T0.5.mat')
after running the function the following is shown
Name =
'K3 d2 D12 V20 S26 t2 C0.8 T0.8.mat'
data =
struct with fields:
pc: [15×51 double]
vc: [15×51 double]
distance: [0 2 4 6 8 10 12]
ans =
struct with fields:
pc: [15×51 double]
vc: [15×51 double]
distance: [0 2 4 6 8 10 12]
the file doesnot appear in the workspace however it makes a data structure
"data=load(filename)" will put all the data contained in the .mat to the variable "data" as a structure;
You can use "load(filename)" in your m-file. It will be the same as typing "load(filename)" in the Command Window.

Sign in to comment.

More Answers (1)

Instead of calling mat2str repeatedly I'd use the + operator for string arrays to build up the filename.
x = 1;
y = 2;
z = 3;
filename = "Article" + x + "_Section" + y + "_Line" + z + ".txt"
filename = "Article1_Section2_Line3.txt"
I would also recommend using fullfile to combine the directory path and file name together rather than concatenating with '\' yourself. What if you were running on a Linux machine, like the machinery used by MATLAB Answers to run code?
theFullPath = fullfile(matlabroot, "another_part_of_file_path", filename)
theFullPath = "/MATLAB/another_part_of_file_path/Article1_Section2_Line3.txt"
You could call load(theFullPath) in MATLAB (if that file existed, but this particular file does not so I won't call load.)

1 Comment

@Steven Lord mat2str is used repeatedly as these are input variables of another function and i am using them for a file name to save a .mat file accordingly for different iterations.

Sign in to comment.

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!