Append apostrophe as a text in a function for a searching tool
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Fangjun Jiang
2022 年 4 月 1 日
Good reference! I was hoping to solve the issue by providing a simple example, not to have to mention "aphostrophe".
採用された回答
Fangjun Jiang
2022 年 4 月 1 日
編集済み: Fangjun Jiang
2022 年 4 月 1 日
Find a .mat file in your current folder and try this:
name='MyData.mat';
folder=pwd;
filename=fullfile(folder,name);
data=load(filename);
2 件のコメント
Fangjun Jiang
2022 年 4 月 1 日
"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.
その他の回答 (1 件)
Steven Lord
2022 年 4 月 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"
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)
You could call load(theFullPath) in MATLAB (if that file existed, but this particular file does not so I won't call load.)
参考
カテゴリ
Help Center および File Exchange で Search Path についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!