楼主: little007
4893 5

请教一个关于 matlab 显示 完整文件路径 的程序问题 [推广有奖]

  • 5关注
  • 0粉丝

大专生

71%

还不是VIP/贵宾

-

威望
0
论坛币
1 个
通用积分
5.4933
学术水平
2 点
热心指数
2 点
信用等级
2 点
经验
729 点
帖子
76
精华
0
在线时间
43 小时
注册时间
2011-6-14
最后登录
2019-3-3

楼主
little007 发表于 2013-5-9 21:47:57 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
由于个人需要,需要编写一个显示文件完整路径的程序,自己尝试了一下,但是出现了一些问题,希望高手指教

程序要求:

输入: 起始文件路径
输出: 文件夹以及其子文件夹中所有文件完整路径
程序名 find_path
例如:
     d盘中有: 文件夹 1 ,2 ,3
                  文件 1.txt  2.txt  3.txt
     文件夹1中有: 文件 a.txt b.txt 文件夹 a b
        文件夹 2 3 a b 都是空的
    运行   find_path('d:\')
    输出结果: d:\1.txt
                     d:\2.txt
                     d:\3.txt
                     d:\1\a.txt
                     d:\1\b.txt
                     d:\1\c.txt
输出各个文件的顺序可以不同

在下主要有两个问题需要解决: 1、函数中数据的存储用什么格式
                                              2、迭代的具体实现

这是我编写的程序:


function full_path=find_path(start_path)
path_name=dir(start_path);
file_found_first=0;
file_found_next=0;
for i=3:length(path_name)
    if path_name(i).isdir
         file_found_next=find_path(strcat(start_path,'\',path_name(i).name));
    else
         file_found_first=strcat(start_path,'\',path_name(i).name);
    end
end
file_found={file_found_first file_found_next};
full_path=file_found;


希望高手不吝赐教。

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:MATLAB matla atlab Lab Atl 文件夹 程序

回帖推荐

Chemist_MZ 发表于2楼  查看完整内容

1. You can save in a cell array and use char() to convert into string when you have further use. 2. You mean you don't know the number of the subdirectory in the path? That is a little complicated. If you only have folder and txt file under the path. You can use dir() to get he dir info, and use .isdir to get the subfolders. Then you again use dir() to get the files and subsubfolders. Th ...

本帖被以下文库推荐

沙发
Chemist_MZ 在职认证  发表于 2013-5-10 00:09:44
1. You can save in a cell array and use char() to convert into string when you have further use.

2. You mean you don't know the number of the subdirectory in the path? That is a little complicated.

If you only have folder and txt file under the path. You can use dir() to get he dir info, and use .isdir to get the subfolders. Then you again use dir() to get the files and subsubfolders.

This way is only for finite number of subdirectories. If you only have the original directory and one layer of subdirectory d:\ and (d:\a d:\b)  I think you can use what I recommend.

best,
已有 1 人评分经验 论坛币 收起 理由
见路不走 + 5 + 5 热心帮助其他会员

总评分: 经验 + 5  论坛币 + 5   查看全部评分

扫头像关注公众号“二点三西格玛”衍生品定价与风险管理

藤椅
little007 发表于 2013-5-10 17:37:35 来自手机
非常感谢您的回答。
不过我还有点儿问题。我的问题跟你说的第二点类似。
我想达到的效果是输入一个路径,函数返回输入路径中所有文件(包括所有子文件夹)的完整路径,如果某个子文件夹是空的,这个子文件夹的路径输出与否都可以。

板凳
little007 发表于 2013-5-10 17:39:37 来自手机
Chemist_MZ 发表于 2013-5-10 00:09
1. You can save in a cell array and use char() to convert into string when you have further use.

...
您说的第一点很好,我下去再试一下

报纸
little007 发表于 2013-5-11 15:08:31
非常感谢matlabsky的smachine
我看了genpath()的源码,类似编了我的函数,达到了目的
我的源码:
find_path.m

function file_path=find_path(start_path)
%------------------------------------------------------
%参考内建函数genpath
%本函数返回所给文件夹中所有文件完整路径
%------------------------------------------------------

file_path=[];
file=dir(start_path);
isfile=logical(~cat(1,file.isdir));
for i=3:length(isfile)
    if isfile(i)
        file_path=[file_path fullfile(start_path,file(i).name) pathsep];
    else
        file_path=[file_path find_path(fullfile(start_path,file(i).name))];
    end
end
end

这样返回的是把找到的所有文件完整路径(包含文件名)连成了一个字符串,字符串之间按平台路径分隔符分割(windows下是;)
后续需要可以用regexp(find_path('your_path'),';','split')分割出来每个路径

地板
Chemist_MZ 在职认证  发表于 2013-5-11 18:28:03
little007 发表于 2013-5-11 15:08
非常感谢matlabsky的smachine
我看了genpath()的源码,类似编了我的函数,达到了目的
我的源码:
Ok, congradulations~
扫头像关注公众号“二点三西格玛”衍生品定价与风险管理

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jr
拉您进交流群
GMT+8, 2025-12-31 20:49