getting string from the command window output

조회 수: 69 (최근 30일)
Mini Me
Mini Me 2013년 2월 27일
Im a bit curious about this. I have a c++ .exe file that I call in matlab using system('command')and pass an input file (input.txt) to it and the result prints out in the command window. Now, I am wondering is there a way to actually scan that results and change it to double or number so I can plot it since nothing was really declared in matlab the results are not saved in the workspace. but I still would like to plot it. Can you help please?
Also any vague idea on how to get a min and max of an elapsed time using tic toc?

답변 (6개)

Jason Ross
Jason Ross 2013년 2월 27일
Use system() like this
[status,result] = system('your_command.exe');
the output will be in "result" -- you may need to perform some function like str2double on the output that you get back -- check the variable type on "result".
to get min and max of an elapsed time, you could store the results of your runs in a vector and use min and max on it.

Mini Me
Mini Me 2013년 3월 4일
Thanks Jason, but when I do that and texts can result then use str2double to get the value I need it return NaN. I'm guessing because the results has character in it. I tried str2num, isnumrric, cell2mat, cell fun none of them work
  댓글 수: 2
Walter Roberson
Walter Roberson 2013년 3월 4일
Use sscanf() or textscan(), or use regexp() to pull out the numbers and then str2double() them.
Keep in mind that another way to proceed would be
system("command > output.txt")
and then you are reduced to the problem of reading a text file.
Jason Ross
Jason Ross 2013년 3월 4일
편집: Jason Ross 2013년 3월 4일
What Walter said ... and make sure if you use the "write a file" method listed, you also take the usual care with files, e.g.
  • Check for write permission and disk space where you are going to write
  • Check that a same file does not already exist (or generate a new name each time)
  • Clean up the file when you are done reading it
  • It's probably also a good idea to check the exit status of your system() command as well, if you are redirecting output. A non-zero status generally means something went wrong and you should probably stop.

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


Mini Me
Mini Me 2013년 3월 4일
Still get the same result after using str2double it returns NaN of 207x1 matrix. I just want to read or scan through the results and get that specific numerical results and save in another file then open that file and then plot it. That's just a vague idea of what I'm trying to acheive
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 3월 4일
str2double() will only read one number at a time and will return errors if there is anything else (other than whitespace) in the string. You need to break out the numeric portions of the string (such as with regexp) in order to use str2double.
sscanf() might return NaN if the data does not match the format specifier you provide.
I suggest you provide us with a sample of the output and indicate which numbers you want to read in.

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


Mini Me
Mini Me 2013년 3월 4일
Sscanf gave the same problem

Jason Ross
Jason Ross 2013년 3월 4일
편집: Jason Ross 2013년 3월 4일
Have you tried using the Import Wizard on the file? Right click, "Import Data", then do what you need to, then under "Import Selection", you can generate a script or function. If you read the code, the import command will be in there (or you can just use the generated code ...)
Barring that, you'd need to post relevant data as without seeing it, it's a guessing game.

Mini Me
Mini Me 2013년 3월 4일
I think I found the problem the result coming out I bad format that may be why it can't convert str2double. Any ideas?

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by