How to import and read Excel input into an executable

Hello everyone
I need help with reading an excel file as an input into my code which i plan to convert to an executable.
I have to read the data from the excel in such a way that the data from the row 1 and column 1 is assigned to a particular variable in the code, there are 88 such variables. I must access the excel interactively from a file location , then read it and assign the value to these variables.
Can anyone help me with the same, i have limited knowledge in matlab and any lead is Highly appreciated.
Thank you

댓글 수: 2

What kind of flexibility do you have on how you access your data? Is this some kind of homework problem that you have to do exactly what they ask?
deep
deep 2018년 12월 18일
I have complete flexibility on accesing the data. No restriction.

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

 채택된 답변

Guillaume
Guillaume 2018년 12월 17일

1 개 추천

readtable will read an excel file and directly transform each column into variables of a table.
I would not recommend having over 80 different variables in any function/script. It would be a complete maintenance nightmare and a clear indication that the design has not been thought out properly. A table would be a lot easier to manipulate.

댓글 수: 6

deep
deep 2018년 12월 18일
Hi Guillaume,
Thank you for your response. I need to have these variables for solving my equations. If any variable is missed out the code does not function. Maximum variable i can have can be limited to around 50. Beyond that i dont see a way to fix them. Else the code will be applicable only for a single case and for a case change i will have to produce a new code. If there is a way out of this i would appreciate if you could help me out on the aspect. Because these variables are tricky to handle as u clearly mentioned.
Thank you
For a start, are all these variables independent? What do they represent? What are you using them for?
Things to consider:
  • If the variables are iteration of the same thing, group them in an array. Never have sequentially named variables. Instead of myvar1, myvar2, myvar3, ... have myvar(1), myvar(2), ...
  • If the variables are somehow related, group them into a structure. e.g. instead of
ambientpressure = xxx;
ambienttemperature = xxx;
ambienthumidity = xxx;
have
environment.pressure = xxx;
environment.temperature = xxx;
environment.humidity = xxx
You then have only one variable, the structure, to pass to functions instead of 3.
deep
deep 2018년 12월 19일
Hi,
The variables are independent.
They represent coefficients and some basic parameters like the dimensions etc
I am using them to solve a set of equations where these variables are iteratively used to get to the solution.
Could you please elaborate on your first point, that will help me in improving my code.
Currently i have modified the code such that these variables are interactively changed using GUI. But i am keen on understanding the approach you mentioned.
Thank you for your suggestions.
deep
deep 2018년 12월 19일
Hi Guillaume,
Thank you for your input.
I have applied the method 1 from your suggestion and that worked just right.
Highly appreciate it!
Could you please elaborate on your first point, that will help me in improving my code.
I think it would be better if you showed an example of your code, then we can see if there's a better way of doing it.
deep
deep 2018년 12월 21일
Sure,
I have attached the file for your perusal.

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

추가 답변 (1개)

Luna
Luna 2018년 12월 17일
Hi,
You can try below to get all excel data.
[fName,pName] = uigetfile({'*.xlsx';'*.xls'},'Please Select an excel file'); % ask you to put excel file and gets file and path names
[num,txt,raw] = xlsread(fullfile(pName,fName)); % reads the excel and gets the numeric as double array, text as cell array and raw data as cell array
var1 = raw{1,1};
var2 = raw{1,2}; % etc...
% OR if all your data is numeric
var1 = num(1,1);
var2 = num(1,2); % etc ...
% you can also use for loops
OR you can also use Activex but it is a bit complicated see link below:

댓글 수: 4

Hi Luna,
I used uiget method for calling the data from excel. It worked .
Here i have a doubt
-How can i write the output into the designated cell in excel, can xlswrite be used here?
Thank you
I have observed that Xlswrite works,
Could you please tell me the reason for recommending ActiveX?
Thank you

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

카테고리

도움말 센터File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

제품

릴리스

R2018b

질문:

2018년 12월 17일

댓글:

2019년 2월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by