Use of evalc with num2str

조회 수: 5 (최근 30일)
Suha
Suha 2017년 12월 11일
댓글: Stephen23 2017년 12월 11일
Hello everyone
I am trying to use evalc to retrieve a file from a folder filled with files.
I have multiple files that are named as follows:
14122017_Rutherford_data_559_F-trigma_1957_008_data.csv
If I do
evalc('dir *data.csv*')
then I am able to get every single file in this folder. However I only want to output files which end with 1957_008_data.csv
Therefore, if I do
evalc('dir *1957_008_data.csv*')
- this works for me.
The issue is: this folder has files for different years (1957 and others) and different codes (008 and others), so I want a way to somehow be able to use evalc to get files where the user selects the pick_year and pick_code. For example the following will give 1957_008_data.csv as output but I don't know how to use eval with this. Could someone help me with the syntax of how to use the following in evalc. Thank you. Su
[num2str(pick_year) '_' num2str(pick_code) '_data.csv']
  댓글 수: 1
Stephen23
Stephen23 2017년 12월 11일
What is the point in using evalc in this code? This seems to serve absolutely no purpose whatsoever, apart from making your code complex, slow, buggy (you cannot make it work, ergo buggy), insecure, and hard to debug (clearly you could not solve this bug yourself).
It looks very much like a case of cargo-cult programming.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2017년 12월 11일
year = 1957;
code = 8;
pattern = sprintf('*%04d_%03d_data.csv');
dinfo = dir(pattern);
filenames = {dinfo.name};
Notice the complete lack of evalc() anywhere in the code.
  댓글 수: 2
Walter Roberson
Walter Roberson 2017년 12월 11일
To get the date information:
dates = cellfun(@(S) sscanf(S, '%d'), filenames);
This assumes that there might be more than one matching date. If the resulting vector is empty then there were no matching files.
Suha
Suha 2017년 12월 11일
Thank you !! All works now.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Steven Lord
Steven Lord 2017년 12월 11일
Do NOT use eval, evalc, or any other function in the eval family. Just call dir as a function with an input argument and an output argument, then iterate through the elements of the struct array returned as output. See the "Find Information in the Return Structure" example on the documentation page for dir for some code you can adapt to your needs.

Suha
Suha 2017년 12월 11일
Thank you Walter and Steven.
Maybe I should have outlined my initial problem that I was trying to solve. I don't necessarily have to use evalc, but being a newbie in MATLAB I'm still trying to find my feet. Here goes:
In a directory filled with files, that are named something like this:
14122017_Rutherford_data_559_F-trigma_1957_008_data.csv
In addition to different years (e.g. 1957) and different codes (e.g. 008), the files have different date info (e.g. 14122017). All I want to do is scan this folder to just extract this date for the file where the pick_year and pick_code specified by the user matches. I think my code is a bit messy as I'm currently using a range of commands (str2double, extractBefore, strtrim and many others) to get this job done.
I don't need to download the file. Is there a more efficient way of scanning a directory and just extracting this date info based on the year and code specified by the user. Could you please let me know.
Thank you !!
Su

카테고리

Help CenterFile Exchange에서 Manage Products에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by