How to save complex number data in single excel file with multiple tabs?

3 vues (au cours des 30 derniers jours)
Greetings
I have a following data
2.0950 + 2.0840i
2.1150 + 2.0950i
2.0910 + 2.0990i
2.0950 + 2.1060i
2.1240 + 2.1000i
2.1080 + 2.1060i
2.0990 + 2.1000i
2.1090 + 2.1010i
2.1150 + 2.1070i
2.1040 + 2.0970i
2.1040 + 2.0970i
2.1210 + 2.0920i
2.1170 + 2.0880i
2.1200 + 2.0890i
2.0990 + 2.0830i
2.0980 + 2.0930i
2.0940 + 2.1020i
2.0960 + 2.0960i
2.1040 + 2.1020i
2.1010 + 2.0920i
2.1060 + 2.0820i
2.1130 + 2.0860i
2.1130 + 2.0930i
2.1060 + 2.0920i
2.0930 + 2.0900i
2.0890 + 2.0900i
2.1090 + 2.0860i
2.1210 + 2.0920i
2.1060 + 2.0900i
2.0980 + 2.0920i
3 groups with 10 complex number
I want to save each group data seperately in different tab(in the above case, we will get three tabs)
below is the brief structure of original codes
clc;
clear;
while i:0<3
[real, imag] = some_external_api_function_dat_brings_out_complex_number();
data = complex(real, imag)
end
and I have tried the below
clc;
clear;
groupnumber=3
filename = 'data.xlsx';
while i:1<=groupnumber
[real, imag] = some_external_api_function_dat_brings_out_complex_number(); % bring out the data
char = strcat('sheet',i)
xlswrite(filename, real, strcat);
xlswrite(filename, imag, strcat);
end
but it isn`t going well
how to solve the above problem

Réponse acceptée

Walter Roberson
Walter Roberson le 6 Oct 2020
while i:1<=groupnumber
You have not defined i at that point, so it is going to have its default value of sqrt(-1)
So your code is equivalent to
while ( (sqrt(-1):1) <= groupnumber)
When you use a complex value with the : operator, MATLAB will give you a warning, but then will take the real() part of the value. So that code becomes equivalent to
while ( (real(sqrt(-1)):1) <= groupnumber)
real(sqrt(-1)) is 0, so the code is equivalent to
while ((0:1) <= groupnumber)
This is a vector test, comparing 0 and 1 to groupnumber. while will consider the resulting condition to be true if all of the results are non-zero. With groupnumber being 3, and 0 and 1 both being <= 3, both parts are true (which is non-zero) and so the while loop would execute the body of the while loop.
As you do not change i or groupnumber inside the loop, it would then be an infinite loop.
char = strcat('sheet',i)
xlswrite(filename, real, strcat);
xlswrite(filename, imag, strcat);
You are not changing i so it is going to have its default value of sqrt(-1) which is a numeric value. When you strcat() a character vector and a numeric value, char() of the numeric value is taken; the process is effectively the same as char(uint16(i)) except that it will explicitly error out because i is complex valued. If you had used strcat('sheet', abs(i)) then abs(-1) would be 1, and that would be the same as ['sheet', char(1)] which would be 'sheet' followed by the SOH (Start of Header) character, and would not be 'sheet1'. You should be looking at using sprintf() instead of strcat() for this purpose.
You are writing both values to the same sheet, using the default range of starting from A1. There are the same number of values in the second case as the first case, so the second set of values would completely overwrite the first set of values.
Excel does not have "tabs". It has rows and columns and sheets. If you want to write to different columns in the same sheet then write [real(:), imag(:)] as the data. If you want to write to different sheets, then you need to modify the name if the sheet between parts.
I just noticed that you are passing strcat as the sheet or range, but strcat is the name of a function that requires at least one parameter, so that would error.
while i:0<3
That would be the same as
while ((i:0)<3)
as described above the i would be sqrt(-1) because you did not assign to it, and sqrt(-1):0 would be treated like (real(sqrt(-1)):0 which would be 0:0 which would be the vector containing 0. Then that 0 would be compared to 3 and the test would be true. As you do not modify i in the loop, that would be an infinite loop as well.
I would suggest to you should probably learn about for loops.

Plus de réponses (1)

Mathieu NOE
Mathieu NOE le 6 Oct 2020
Hi
SEE ATTACHEMENT
this should work. I corrected your code using also some info from another submission :
hope it helps
cheers

Catégories

En savoir plus sur MATLAB Functions in Microsoft Excel dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by