write a xlsx file
Show older comments
Hello folks,
I have a mat file structed as follow and I would like to convert it to xls format.
k = load('psnr_ssim_Stokes_Double_Difference.mat')
k =
struct with fields:
psnr_value_I: [7-D double]
psnr_value_Q: [7-D double]
psnr_value_U: [7-D double]
ssim_value_I: [7-D double]
ssim_value_Q: [7-D double]
ssim_value_U: [7-D double]
I used this code but it didn't give me the content of the variables in a columns I got them in row. This is the code that I used
filename = 'psnr_ssim.xlsx'
T = table(ssim_value_I,psnr_value_I,ssim_value_Q,psnr_value_Q,ssim_value_U,psnr_value_U)
writetable(T,filename)
12 Comments
Scott MacKenzie
on 21 Jul 2021
Edited: Scott MacKenzie
on 21 Jul 2021
I'm a bit confused by your code. You are creating a table by specifying only the fields of the structure. That should generate a compile error. You need
T = table(k.ssim_value_I, . . . );
or just
T = table(k);
It might help if you post your data or post the complete code you are using along with any errors generated.
assia assia
on 21 Jul 2021
Edited: assia assia
on 21 Jul 2021
Walter Roberson
on 21 Jul 2021
Probably not
T = table(k);
More likely would be
T = struct2table(k);
which would create a table with one row, with variables named after the field names, and with each entry being a 7D array.
Walter Roberson
on 21 Jul 2021
If you create a table object containing variables that are not vectors, then when you go to write it out using writetable(), each column will be made into a new variable, like
array2table(reshape(k.psnr_value_I, size(k.psnr_value_I,1), []))
but with variable names derived from the original variable name.
Are you sure that is what you want?
Excel really is not designed for arrays with more than 2 dimensions.
assia assia
on 21 Jul 2021
Walter Roberson
on 21 Jul 2021
if a==0.8500000000000001
Never compare floating point numbers for equality, except under the following circumstances:
- in some cases, it is important to know if the number is exactly 0, such as to prevent division by 0
- You can compare for equality if both numbers are derived from the same source, such as testing (x == min(x))
- if the floating point numbers are all really integers and are never calculated as non-integers, then you can compare the values for equality. For example, never (0:1/3:1)*3 compared to an integer, as the 1/3 suffers floating point round-off. If you need 0:1/3:1 then it is safer to use (0:3)/3
- comparing to literal 0.125, 0.25, 0.375, 0.5, 0.625, 0.75, 0.875 are safe, but only provided that the values were set by literals, not by calculations that "should" mathematically result in one of the values
So, instead of
tmp_a =1;
for a= [0.2,1.5,0.8500000000000001]
stuff
tmp_a = tmp_a + 1;
end
use something like
a_vals = [0.2,1.5,0.8500000000000001];
for tmp_a = 1 : length(a_vals)
a = a_vals(tmp_a);
stuff
end
and then instead of comparing a==0.8500000000000001 compare indices, tmp_a == length(a_vals)
assia assia
on 21 Jul 2021
Walter Roberson
on 21 Jul 2021
You can move all the lines similar to
a_vals = [0.2,1.5,0.8500000000000001];
to before the loops.
You will find that it often helps to calculate the number of elements in one of those vectors, and then loop to that variable, such as
num_a = length(a_vals);
stuff
ssim_value_I = zeros(num_g, num_r, num_a, num_i, num_p, num_e, num_w);
stuff
for temp_a = 1 : num_a
a = a_vals(tmp_a);
stuff
end
assia assia
on 21 Jul 2021
Walter Roberson
on 21 Jul 2021
What is your current code for creating the table object?
assia assia
on 21 Jul 2021
Edited: assia assia
on 21 Jul 2021
Walter Roberson
on 21 Jul 2021
Is "data.mat" the same file as in
save('psnr_ssim_Stokes_Linear_Seperable.mat','ssim_value_I','psnr_value_I','ssim_value_Q','psnr_value_Q','ssim_value_U','psnr_value_U')
? If so then 'ssim_value_I' is length(g_vals) by 6 other dimensions, and you have two different g_vals, so ssim_value_I would be 2 x 6 other dimensions. When you do the struct2table() the 2 x something becomes a table with two rows.
You can convert to individual variables by using splitvars(); see https://www.mathworks.com/help/matlab/ref/table.splitvars.html
... but keep in mind that you will then have a table with a lot of different columns, each of which is only 2 rows long.
I would suggest to you that you should be considering permute() and then reshape() to get the data in the order you want.
Answers (0)
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!