How to import variable names array from Excel as symbolic variables?

I have defined some symbolic variables, let's say by following means:
%%Declaration of all possible symbolic variables based on total number of streams
n_stream = 38; % total no. of streams in the flowsheet
for k=1:n_stream
syms(sprintf('Qs%d', k)) % Solid flow rate
syms(sprintf('Ql%d', k)) % Liquid flow rate
syms(sprintf('Cw%d', k)) % Solid weight fraction in flow
syms(sprintf('CsV%d', k)) % Solid V content
syms(sprintf('ClV%d', k)) % Liquid V content
syms(sprintf('CsZn%d', k)) % Solid Zn content
syms(sprintf('ClZn%d', k)) % Liquid Zn content
end
Now, I want to give the initial value of these variables for an optimization problem. So, what I need to provide are name of variables ordered in a array and a vector of its initial values. They are written in excel file in something like below manner:
What I want to is to report the variable names and its initial values as two different vectors. I am able to successfully import the initial values from excel file by xlsread command. However, I have tried importing the variable names by the different way but it results in cell values instead of symbolic variable names, something like below:
[num txt raw] = xlsread('../../NALCOProjectExcelDoNotRelocate','Sheet1')
txt =
'Qs1'
'Qs14'
'Qs16'
'Qs30'
'Qs33'
'Qs37'
'Ql3'
'Ql4'
'Ql8'
'Ql9'
'Ql11'
'Ql17'
'Ql20'
'Ql28'
'Ql29'
'Ql32'
'Ql35'
'Cw7'
'Cw12'
'Cw13'
'Cw14'
'Cw15'
'Cw16'
'Cw18'
'Cw26'
'Cw31'
'Cw33'
Similar, results for raw type as well.
Also, tried file reading and then writing the code in txt file format. But it gives writing errors, something like below:
[numData textData rawData] = xlsread('../../NALCOProjectExcelDoNotRelocate','Sheet1')
save('pqfile.m', 'rawData', '-ASCII')
new_file = 'pqfile.m'
eval(new_file)
Error:
Warning: Attempt to write an unsupported data type to an ASCII file.
Variable 'rawData' not written to file.
What I understood from reading over internet that this way I can only write numeric data.
Please help me in troubling the problem. Simply saying, I have defined symbolic variables and want to import the vector of this variable names from excel file.
Thanks in advance and regards...

1 Comment

Do you really need symbolic variables? It would be quite easy and efficient to import that data and convert it into a structure of numeric values, which you could thus access simply:
S.QS1
S.QS14
...etc

Sign in to comment.

 Accepted Answer

Why symbolic variables; why not simple variables?
It's generally an indication of a lack of structure in an algorithm to have so many sequentially-numbered variable names rather than using an array. What's the actual problem to be solved; can it not be written in matrix form so can bring the power of Matlab vectorized syntax to it?
You can read/write the variables in a form that an m-file can be parsed to read them; it would look something like
[num txt]=xlsread('../../NALCOProjectExcelDoNotRelocate','Sheet1');
fid=fopen('pqfile.m','w');
for i=1:length(num)
fprintf(fid,'%s=%.15g;\n',txt{i},num(i))
end
fid=fclose(fid);
but I really doubt this is the most efficient way to structure your problem going forward.

3 Comments

Thanks, DB for the answer. The whole purpose is to develop a software where the different type of plant flowrates are going to take part. As a developer I can easily avoid the use of symbolic variables, however, the plant operator may face difficulty in doing so. That is why I am taking help of self-explanatory symbolic variables.
Your solution worked as dictated for the writing purpose. However, when I am trying to read it back to the program with it is not expected. Function eval with file name is not working. Commands fread or fscan or fgets is reading but the vector structure is completely changed. It has more size (size equal to the number of characters) than the number of symbolic variables. Any way out from this.
It's a script m-file; just execute it like any other ML function/script.
You really, really do NOT want to try to write a complex application this way; it will become totally unmaintainable "real soon now".
At most, use something like struct with named fields that can be modified programmatically; at best use the user-defined names as way to index into arrays and use the vector ability of ML as designed/intended for the actual computations.
You've not given enough background for any of us here to really know how that could look specifically.
Also the description of the reason for using "symbolic" variables is the common language interpretation of the term symbolic as being a recognizable variable name visually as opposed to the Matlab symbolic toolbox use of the meaning of "symbolic" as the variable type.
The point is you can refer to the variables externally via the name convention you wish to set up for the operator but there is no need whatsoever (and much difficulty in implementation to do so as you're discovering) to keep those names all specifically in the code; use a translation/lookup table to refer from the user variables to the internal ones, NOT an attempt to write dynamically-defined variable names; "there be dragons!" that will inevitably sink your ship.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 5 Aug 2018

Commented:

dpb
on 6 Aug 2018

Community Treasure Hunt

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

Start Hunting!