matlab读取raw文件

这段代码展示了使用MATLAB读取.raw格式图像文件,进行图像重塑、显示,以及循环读取一系列数字对应的.raw文件,并将处理后的图像数据存储到三维矩阵中。此外,还涉及将矩阵写入.dat文件,以及从Excel文件中读取特定范围的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读文件

clc;clear all;
id = fopen('file.raw','r');
A = fread(id,'uint16');
imgsize = size(A);%读出的图像的size为n*1大小
rows = 512;%根据mhd文件的图像大小设定
clos =512;
nums = imgsize(1)/rows/clos; %计算得到图像张数
I = reshape(A,[rows,clos,nums]);
single_image = reshape(I(:,:,2),[rows,clos]);%显示第3张
B=uint16(single_image);%转化为unit8显示
B=B';%求图像
imtool(B);

循环读文件,判断数字是几位数字

for i = 0:730
   temp = num2str(i);
   if(length(temp) == 1)
       str1 = ['000',temp];
   elseif(length(temp) == 2)
       str1 = ['00',temp];
    elseif(length(temp) == 3)
       str1 = ['0',temp];
   end

    root = [obj_root,'\',str1,'.raw'];
    fileID = fopen(root,'r');
    A = fread(fileID,'uint16');
    fclose(fileID);
    rows = Slice;%根据mhd文件的图像大小设定
    clos = Detector_num;
    imgsize = size(A,1);%读出的图像的size为n*1大小
    single_image = reshape(A,[rows,clos]);
% %     single_image = reshape(A,[clos,rows]); wrong
% %     imtool(single_image);
    obj_I_120kv(i+1,:,:) = single_image;
end

将三维矩阵写入.dat文件

[depth,height, width] = size(total_kev_Imgae);

% 打开文件
fid = fopen('noScatter.dat', 'wb');
% 检查文件是否成功打开
if fid == -1
    error('Cannot open file for writing.');
end

% 将三维矩阵写入文件
for i = 1:depth
    fwrite(fid, total_kev_Imgae(i, :, :), 'float');
end

% 关闭文件
fclose(fid);

读取excel中指定范围的数据

withScatter = readtable('with scatter.xlsx', 'Range', 'B1:C56');
withScatter_mean= double(withScatter.mean);
withScatter_std= double(withScatter.std);

在这里插入图片描述

评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值