フィルターのクリア

How to read arrays from files?

1 回表示 (過去 30 日間)
David Pesetsky
David Pesetsky 2016 年 3 月 24 日
回答済み: Walter Roberson 2016 年 3 月 25 日
Hi. I inherited some code that I want to compile into an executable. It has a run() statement that brings in some data, and the compiler i guess doesn't want to compile run().
run(file.m);
File contents:
Headers = {...
'a' 'b' 'c' 'd' 'e' 'f' 'g' 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' 'x' 'y' 'z' 'aa'
};
TowerData = {...
'TwrBaseFX' 'TwrBaseFY' 'TwrBaseFZ' 'TwrBaseFR' 'TwrBaseMX' 'TwrBaseMY' 'TwrBaseMZ' 'TwrBaseMR' 1 0.745 8.475 4.300 0.022 21.925 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 0 0 0 0 0
'TwrBaseFX' 'TwrBaseFY' 'TwrBaseFZ' 'TwrBaseFR' 'TwrBaseMX' 'TwrBaseMY' 'TwrBaseMZ' 'TwrBaseMR' 1 0.745 8.475 4.300 0.022 21.925 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 1 0.85 2.26 0.3 0.03
'TwrS02FX' 'TwrS02FY' 'TwrS02FZ' 'TwrS02FR' 'TwrS02MX' 'TwrS02MY' 'TwrS02MZ' 'TwrS02MR' 1 8.475 16.205 4.300 0.018 21.925 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 0 0 0 0 0
'TwrS03FX' 'TwrS03FY' 'TwrS03FZ' 'TwrS03FR' 'TwrS03MX' 'TwrS03MY' 'TwrS03MZ' 'TwrS03MR' 1 16.205 23.935 4.300 0.016 21.925 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 0 0 0 0 0
'TwrS04FX' 'TwrS04FY' 'TwrS04FZ' 'TwrS04FR' 'TwrS04MX' 'TwrS04MY' 'TwrS04MZ' 'TwrS04MR' 2 23.935 31.665 4.122 0.015 25.912 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 0 0 0 0 0
'TwrS05FX' 'TwrS05FY' 'TwrS05FZ' 'TwrS05FR' 'TwrS05MX' 'TwrS05MY' 'TwrS05MZ' 'TwrS05MR' 2 31.665 39.395 3.853 0.013 25.912 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 0 0 0 0 0
'TwrS06FX' 'TwrS06FY' 'TwrS06FZ' 'TwrS06FR' 'TwrS06MX' 'TwrS06MY' 'TwrS06MZ' 'TwrS06MR' 2 39.395 47.125 3.585 0.012 25.912 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 0 0 0 0 0
'TwrS07FX' 'TwrS07FY' 'TwrS07FZ' 'TwrS07FR' 'TwrS07MX' 'TwrS07MY' 'TwrS07MZ' 'TwrS07MR' 3 47.125 54.855 3.332 0.012 29.463 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 0 0 0 0 0
'TwrS08FX' 'TwrS08FY' 'TwrS08FZ' 'TwrS08FR' 'TwrS08MX' 'TwrS08MY' 'TwrS08MZ' 'TwrS08MR' 3 54.855 62.585 3.112 0.011 29.463 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 0 0 0 0 0
'TwrS09FX' 'TwrS09FY' 'TwrS09FZ' 'TwrS09FR' 'TwrS09MX' 'TwrS09MY' 'TwrS09MZ' 'TwrS09MR' 3 62.585 70.315 2.893 0.011 29.463 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 0 0 0 0 0
'TwrTopFX' 'TwrTopFY' 'TwrTopFZ' 'TwrTopFR' 'TwrTopMX' 'TwrTopMY' 'TwrTopMZ' 'TwrTopMR' 3 70.315 78.045 2.674 0.011 29.463 0.600 25.000 1.000 0.200 1.000 0.650 0.400 0.600 0 0 0 0 0
};
So I just want to replace the run() with a similar command to read these 2 arrays from a text file that loads Headers and TowerData the same as the Run() did.
I cannot put the data into the code to compile, because it can vary in length and content. I was playing with fscanf, but it seemed to mess up on the strings.
  12 件のコメント
Walter Roberson
Walter Roberson 2016 年 3 月 25 日
evalin() cannot be used in compiled code to read outside code.
Remember, this question has to do with reading something user-alterable that is structured like code, at run-time, within a compiled application. The MATLAB Compiler is designed so that the parser is not present, so any call to eval() or evalin() or evalc() or run() is going to fail. All code must be known to MATLAB Compiler at the time of compiling. Anything that must be loaded at run-time according to user changes must be treated as a data file that has to be analyzed by code.
David Pesetsky
David Pesetsky 2016 年 3 月 25 日
The original data above can be put in similar formats...it's basically a table with a header and labels on each row. So a mix of string and text. What's a good format, and method to read, such that I get the same 2 resulting arrays?

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 3 月 25 日
I suggest you use readtable() on a csv if you have R2013b or later.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by